Skip to content

fix(sql-orm-client): deduplicate foreign keys in include query IN clause - #31171

Open
JonathanHerSa wants to merge 1 commit into
prisma:mainfrom
JonathanHerSa:fix/deduplicate-include-foreign-keys-31122
Open

JonathanHerSa wants to merge 1 commit into
prisma:mainfrom
JonathanHerSa:fix/deduplicate-include-foreign-keys-31122

Conversation

@JonathanHerSa

@JonathanHerSa JonathanHerSa commented Nov 22, 2026 •

Copy link
Copy Markdown

Linked issue

Closes #31122

Summary

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)
  • pnpm --filter @internal/sql-orm-client lint (biome check clean)

Skill update

n/a — internal only

Checklist

  • 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.

Closes prisma#31122

Signed-off-by: Jonathan Hernandez Sanchez <[email protected]>
@JonathanHerSa
JonathanHerSa requested a review from a team as a code owner November 22, 2026 05:11
@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

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.

Changes: Shared-column filter deduplication

Layer / File(s) Summary
Deduplicate shared-column values
packages/3-extensions/sql-orm-client/src/statement-graph/filtered-statement.ts, packages/3-extensions/sql-orm-client/test/statement-graph/filtered-statement.test.ts
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.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested reviewers: wmadden-electric

Merge Risk: Minimal · up to 9acdd

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: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed 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 Passed 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 Passed 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
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: prisma/orm
  • Branch: main
  • File: .coderabbit.yml
  • Commit: 033e8dc
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: fd7863b7-d3b5-4806-a8c4-787607f34fd7
Commits

Reviewing files that changed from the base of the PR and between 033e8dc and 9acddf1.

Files selected for processing: 2

  • packages/3-extensions/sql-orm-client/src/statement-graph/filtered-statement.ts
  • packages/3-extensions/sql-orm-client/test/statement-graph/filtered-statement.test.ts

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Include query repeats duplicate foreign keys in the IN list (7.x; 6.x deduplicates)

1 participant