You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(sql-orm-client): deduplicate foreign keys in include query IN clause - #31171
When loading relations with include (e.g. a findMany on a model with a to-one relation where multiple parent rows reference the same related key), duplicate foreign key parameters were being placed into the SQL IN list (WHERE id IN ($1, ..., $50) with identical values). This updates anyOf in @internal/sql-orm-client to deduplicate ParamRef items before generating BinaryExpr.in, matching the deduplicated behavior of Prisma 6.x.
Testing performed
pnpm vitest run test/statement-graph/filtered-statement.test.ts (all 41 tests passed, including new deduplication and single-distinct-value tests)
pnpm vitest run test/statement-graph/ (50 test files passed, 604 tests passed, 0 errors)
All commits are signed off (git commit -s) per the DCO. The DCO status check will block merge if any commit is missing a Signed-off-by: trailer.
Tests are updated (or n/a if the change is doc-only / refactor with no behavioural delta).
For a bug fix: I searched for the same bug elsewhere and fixed every occurrence, per Fix the cause, everywhere it occurs (or n/a if this is not a bug fix).
The Skill update section above is filled in (or stated n/a — internal only).
Notes for the reviewer
Deduplication uses the existing stitchKey utility to hash parameter values by identity, safely falling back to type-prefixed strings. If multiple conditions share the same value, anyOf emits IN ($1) rather than 50 repeated placeholders.
Summary by CodeRabbit
Bug Fixes
Filters that compare the same column with multiple values now include each distinct value only once in the generated matching criteria.
Conditions that do not share a column continue to use the existing behavior.
When loading relations via include (such as a findMany with a to-one relation where multiple rows reference the same parent), the query generates an IN list containing duplicate parameter values for the foreign keys.
Update anyOf to deduplicate ParamRef items before assembling the BinaryExpr.in expression, ensuring distinct foreign keys in the SQL IN clause and matching the deduplicated behavior of Prisma 6.x.
Closesprisma#31122
Signed-off-by: Jonathan Hernandez Sanchez <[email protected]>
No actionable comments were generated in the recent review.
Walkthrough
anyOf now deduplicates parameter values when same-column conditions produce an IN list. Tests cover duplicate values in filter execution and direct anyOf calls.
The shared-column anyOf path removes duplicate parameter values from generated IN lists. Tests cover duplicate conditions, a single distinct value, and a single condition returned unchanged.
Include follow-up queries now put each distinct related key in the SQL IN list once, which reduces bind-parameter use and planning work. The change is localized and has targeted tests. No concrete merge-blocking risk remains.
Pre-merge checks: 4
Check name
Status
Explanation
Description Check
Check skipped - CodeRabbit’s high-level summary is enabled.
Title check
The title names the main change: deduplicating foreign-key values in the include query IN clause. The raw summary confirms anyOf now removes duplicate parameter values from the IN list, and the linked…
Linked Issues check
Issue #31122 requires that the include follow-up query place each distinct related key in the SQL IN list once. The PR adds deduplicateParams in anyOf (filtered-statement.ts). When all conditions comp…
Out of Scope Changes check
The whole-PR changes (merge base 033e8dc to head 9acddf1) touch only filtered-statement.ts and its test file. The source change adds the stitchKey import, the deduplicateParams and paramKey helpers,…
Full details: Title check
Explanation
The title names the main change: deduplicating foreign-key values in the include query IN clause. The raw summary confirms anyOf now removes duplicate parameter values from the IN list, and the linked objectives describe the same fix for repeated keys in include follow-up queries. The title is specific, follows the conventional commit style, and is 72 characters, so it is concise enough.
Full details: Linked Issues check
Explanation
Issue #31122 requires that the include follow-up query place each distinct related key in the SQL IN list once. The PR adds deduplicateParams in anyOf (filtered-statement.ts). When all conditions compare the same column with a parameter, anyOf now builds BinaryExpr.in from unique ParamRef values. Duplicate keys from multiple parent rows therefore produce one placeholder, so the repro case yields IN ($1) rather than 50 placeholders. Deduplication uses stitchKey on the value, prefixed by typeof, so distinct values of different types are not merged. The PR also adds tests for duplicate values and for a single distinct value, as stated in the PR description and the test summary. The issue has no further acceptance criteria. I did not trace the full include path to confirm that every include follow-up reaches anyOf, but the filter path in filteredWhere uses anyOf for each KeyMatch group, which matches the reported SQL shape.
Full details: Out of Scope Changes check
Explanation
The whole-PR changes (merge base 033e8dc to head 9acddf1) touch only filtered-statement.ts and its test file. The source change adds the stitchKey import, the deduplicateParams and paramKey helpers, and the call site in anyOf. All of these serve the deduplication objective. The test additions cover the same objective. No unrelated code, refactoring, or documentation changes appear in the reviewed summary or diff. Residual risk, not a scope issue: when stitchKey throws, paramKey falls back to typeof plus String(value). Two distinct non-primitive values with the same String form would be merged and one key would be dropped from the IN list. This does not affect the reported case of scalar foreign keys.
Finishing Touches
Generate unit tests (beta)
Create a new PR
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Linked issue
Closes #31122
Summary
When loading relations with
include(e.g. afindManyon a model with a to-one relation where multiple parent rows reference the same related key), duplicate foreign key parameters were being placed into the SQLINlist (WHERE id IN ($1, ..., $50)with identical values). This updatesanyOfin@internal/sql-orm-clientto deduplicateParamRefitems before generatingBinaryExpr.in, matching the deduplicated behavior of Prisma 6.x.Testing performed
pnpm vitest run test/statement-graph/filtered-statement.test.ts(all 41 tests passed, including new deduplication and single-distinct-value tests)pnpm vitest run test/statement-graph/(50 test files passed, 604 tests passed, 0 errors)pnpm --filter @internal/sql-orm-client lint(biome check clean)Skill update
n/a — internal only
Checklist
git commit -s) per the DCO. The DCO status check will block merge if any commit is missing aSigned-off-by:trailer.n/aif the change is doc-only / refactor with no behavioural delta).n/aif this is not a bug fix).n/a — internal only).Notes for the reviewer
Deduplication uses the existing
stitchKeyutility to hash parameter values by identity, safely falling back to type-prefixed strings. If multiple conditions share the same value,anyOfemitsIN ($1)rather than 50 repeated placeholders.Summary by CodeRabbit