Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nested some filters should work #4968

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,70 @@ mod many_relation {
Ok(())
}

fn schema_23742() -> String {
let schema = indoc! {
r#"model Top {
id Int @id

middleId Int?
middle Middle? @relation(fields: [middleId], references: [id])

bottoms Bottom[]
}

model Middle {
id Int @id
bottoms Bottom[]

tops Top[]
}

model Bottom {
id Int @id

middleId Int?
middle Middle? @relation(fields: [middleId], references: [id])

tops Top[]
}"#
};

schema.to_owned()
}

// Regression test for https://github.com/prisma/prisma/issues/23742
#[connector_test(schema(schema_23742))]
async fn prisma_23742(runner: Runner) -> TestResult<()> {
run_query!(
&runner,
r#"mutation {
createOneTop(data: {
id: 1,
middle: { create: { id: 1, bottoms: { create: { id: 1, tops: { create: { id: 2 } } } } } }
}) {
id
}}"#
);

insta::assert_snapshot!(
run_query!(&runner, r#"{
findUniqueTop(where: { id: 1 }) {
middle {
bottoms(
where: { tops: { some: { id: 2 } } }
) {
id
}
}
}
}
"#),
@r###"{"data":{"findUniqueTop":{"middle":{"bottoms":[{"id":1}]}}}}"###
);

Ok(())
}

async fn test_data(runner: &Runner) -> TestResult<()> {
runner
.query(indoc! { r#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,10 @@ fn extract_filter_scalars(f: &Filter) -> Vec<ScalarFieldRef> {
Filter::Scalar(x) => x.scalar_fields().into_iter().map(ToOwned::to_owned).collect(),
Filter::ScalarList(x) => vec![x.field.clone()],
Filter::OneRelationIsNull(x) => join_fields(&x.field),
Filter::Relation(x) => join_fields(&x.field),
Filter::Relation(x) => vec![join_fields(&x.field), extract_filter_scalars(&x.nested_filter)]
.into_iter()
.flatten()
.collect(),
Comment on lines +651 to +654
Copy link
Contributor Author

@Weakky Weakky Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relationJoins queries generate multi-level select statements. Filters are done at a specific level, which requires the select builder to ensure the most nested select statements projects all the fields required for filters to be applied at a higher level. In the case of some, we were not projecting the scalar fields used in relational filters.

eg:

where: { to-many: { some: { id: 1 } } }

id field needs to be selected from inner selects.

_ => Vec::new(),
}
}
Expand Down
Loading