Skip to content

sql-orm-client: update and delete filtered related rows on many-to-many relations in update() - #31016

Merged
SevInf merged 4 commits into
mainfrom
nested-mutation-many-to-many-writes
Nov 13, 2026
Merged

SevInf merged 4 commits into
mainfrom
nested-mutation-many-to-many-writes

Conversation

@StevenMcClankerton

@StevenMcClankerton StevenMcClankerton commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Inside update(), updateAll() and deleteAll() now work on many-to-many relations: they write only the target rows linked to the parent through the junction. Third part of slice filtered-many-writes of the nested-mutations project (spec: projects/nested-mutations/slices/filtered-many-writes/spec.md). Stacked on #31014.

await db.Post.where({ id: 1 }).update({
  tags: (t) => t.where({ name: { startsWith: 'x' } }).updateAll({ hidden: true }),
});
// UPDATE tags SET hidden = true
//   WHERE tags.name LIKE 'x%'
//     AND tags.id IN (SELECT j."B" FROM "_PostToTag" j WHERE j."A" = 1)

Changes

  • Translation (mutation-graph/nested-mutations.ts): a many-to-many updateAll/deleteAll goes through the same addUpdateAll/addDeleteAll as the one-to-many and top-level ones. The related rows' description gets one more filter, target.key IN (SELECT j.target_fk FROM junction j), with the junction under its own alias, and the parent's FilterData edge targets j.parent_fk. There is no junction Find node and no rule. The interim many-to-many rejection from sql-orm-client: update and delete filtered related rows on one-to-many relations in update() #31014 is removed. deleteAll() does not touch junction rows; the database's foreign-key action decides (with keys that do not cascade, its error rolls back the whole update).
  • Edge conditions placed by alias (mutation-graph/filtered-statement.ts): a FilterData edge's condition goes into the SELECT whose FROM alias is the alias of the edge's target column: the outer WHERE for the statement's own tables, as every edge does today, or the WHERE of the subquery that reads that alias. An alias found in neither is an InternalError. Find, Update (both paths) and Delete use it.
  • Row-value IN (Postgres, SQLite and MySQL adapters): a composite target key is (t.k1, t.k2) IN (SELECT j.k1, j.k2 FROM …), which the SQL AST already expresses as a ListExpression IN a SubqueryExpr. The renderers used to refuse any IN subquery of more than one column; they now accept one with as many columns as the row value and refuse a mismatch with RUNTIME.AST_INVALID.
  • Order: the write reads the junction's table state through its subquery, so a connect before an updateAll in the same array is applied first and the updateAll sees the new link.

Why

Design record D8 in projects/nested-mutations/mutation-graph.md chose IN (SELECT …) because a bound list of linked keys grows with the relation and exceeds bound-value limits. D8 reached it through a junction Find inlined by a peephole; the parent's key is only known at run time and reaches nodes through FilterData edges, so either way an edge's condition has to be placed inside a subquery. Building the subquery in the translation needs only that placement, without a junction node, an inlining rule, or rules that run on databases without RETURNING. A new AST form for row-value IN was not needed: the AST already had one, and only the renderers refused it.

Verification

  • @internal/sql-orm-client: typecheck, lint, 2008 tests (graph tests of the subquery write and of connect then updateAll with state edges; placement tests for Update and Delete and the unknown-alias error).
  • Postgres, SQLite and MySQL adapters: typecheck, lint, tests, including row-value rendering and the count mismatch.
  • Integration on PGlite: a matching tag linked only to another user is unchanged; deleteAll() with cascading and with non-cascading junction keys (rolled back); connect then updateAll; a composite-key target. On SQLite: another user's tag unchanged, cascading deleteAll(), a composite-key target. MySQL facade suite against a local mysql:8.4 (188 passed, 1 skipped).
  • Build, lint:deps, lint:ratchets (casts unchanged), check:upgrade-coverage against sql-orm-client: update and delete filtered related rows on one-to-many relations in update() #31014.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Filtered updateAll() and deleteAll() operations are supported for many-to-many relations, including relations with composite keys.
    • Row-value IN subqueries are supported when the subquery returns the same number of columns as the compared row.
  • Bug Fixes
    • Filter conditions are applied to the relevant nested query, improving the accuracy of filtered writes.
    • Subqueries with mismatched column counts now report a clear error.

@StevenMcClankerton
StevenMcClankerton requested a review from a team as a code owner November 12, 2026 18:18
@coderabbitai

coderabbitai Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • Resume reviews
  • Trigger review

No actionable comments were generated in the recent review.

Walkthrough

Filtered nested writes now support many-to-many relations through junction-table filters. Mutation graphs carry link conditions into update and delete statements. MySQL, PostgreSQL, and SQLite validate subquery projection widths against row-value operands.

Changes: Filtered nested writes

Layer / File(s) Summary
Build and execute junction filters
packages/3-extensions/sql-orm-client/src/mutation-graph/*, packages/3-extensions/sql-orm-client/test/mutation-graph/*
Mutation graph inputs now carry junction link sources into updates and deletes. Filtered execution combines link conditions with existing conditions, and graph state includes linked junction tables. Unit tests cover link matching, graph state, and filtered statement behavior.
Validate filtered relation writes
test/integration/test/sql-orm-client/mn-*, upgrade-instructions/pending/nested-mutation-many-to-many-writes/extension/instructions.md
Integration tests cover filtered many-to-many updates and deletes, composite keys, operation ordering, junction-row behavior, cascades, and rollback.

Row-value subqueries

Layer / File(s) Summary
Validate row-value subquery widths
packages/3-targets/mysql/1-mysql/2-adapter/..., packages/3-targets/postgres/2-adapter/..., packages/3-targets/sqlite/2-adapter/...
Each adapter validates a subquery projection against the row-value operand width. Tests cover matching widths and mismatches.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant NestedMutations
  participant MutationGraph
  participant FilteredStatement
  participant Database
  NestedMutations->>MutationGraph: Supply filters and junction link sources
  MutationGraph->>FilteredStatement: Pass filter and link outputs
  FilteredStatement->>Database: Execute statement with combined conditions
Loading

Suggested reviewers: sevinf, wmadden-electric

Merge Risk: Minimal · up to f7815

This change adds updateAll() and deleteAll() on many-to-many relations inside update(), scoped to rows linked through the junction table. No concrete merge-blocking issue was found in the reviewed code.

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 sql-orm-client package and the many-to-many filtered updateAll()/deleteAll() behavior inside update(). The raw summary shows changes to filtered-statement, nested-mutations, update…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the sql-orm-client package and the many-to-many filtered updateAll()/deleteAll() behavior inside update(). The raw summary shows changes to filtered-statement, nested-mutations, update, delete, junction-filter-data, and the Postgres, SQLite, and MySQL renderers that add junction-link filtering for these writes. The title covers the main change, though it omits the renderer row-value IN subquery work.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • 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: 5035c6e
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3d342609-1d8e-4e1a-a90f-54f1bb04c407
Commits

Reviewing files that changed from the base of the PR and between 68df656 and f781591.

Files ignored due to path filters: 3

  • projects/nested-mutations/mutation-graph.md is excluded by !projects/**
  • projects/nested-mutations/slices/filtered-many-writes/spec.md is excluded by !projects/**
  • projects/nested-mutations/spec.md is excluded by !projects/**
Files selected for processing: 17

  • packages/3-extensions/sql-orm-client/src/mutation-graph/collection-graphs.ts
  • packages/3-extensions/sql-orm-client/src/mutation-graph/delete-rule.ts
  • packages/3-extensions/sql-orm-client/src/mutation-graph/delete.ts
  • packages/3-extensions/sql-orm-client/src/mutation-graph/filtered-statement.ts
  • packages/3-extensions/sql-orm-client/src/mutation-graph/graph.ts
  • packages/3-extensions/sql-orm-client/src/mutation-graph/junction-filter-data.ts
  • packages/3-extensions/sql-orm-client/src/mutation-graph/model-statements.ts
  • packages/3-extensions/sql-orm-client/src/mutation-graph/nested-mutations.ts
  • packages/3-extensions/sql-orm-client/src/mutation-graph/update.ts
  • packages/3-extensions/sql-orm-client/test/mutation-graph/delete-rule.test.ts
  • packages/3-extensions/sql-orm-client/test/mutation-graph/filtered-statement.test.ts
  • packages/3-extensions/sql-orm-client/test/mutation-graph/filtered-writes-graph.test.ts
  • packages/3-extensions/sql-orm-client/test/mutation-graph/graph.test.ts
  • packages/3-extensions/sql-orm-client/test/mutation-graph/junction-filter-data.test.ts
  • packages/3-extensions/sql-orm-client/test/mutation-graph/print-graph.ts
  • packages/3-extensions/sql-orm-client/test/mutation-graph/update-without-returning.test.ts
  • packages/3-extensions/sql-orm-client/test/mutation-graph/update.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 6 remain after this review.

@github-actions

github-actions Bot commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

size-limit report 📦

Path Size
postgres / no-emit 287.96 KB (+0.14% 🔺)
postgres / emit 252.13 KB (+0.14% 🔺)
mongo / no-emit 235.84 KB (0%)
mongo / emit 191.51 KB (0%)
cf-worker / no-emit 350.68 KB (+0.13% 🔺)
cf-worker / emit 310.68 KB (+0.14% 🔺)

@pkg-pr-new

pkg-pr-new Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@prisma/orm-extension-arktype-json

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-extension-arktype-json@31016

@prisma/orm-extension-middleware-cache

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-extension-middleware-cache@31016

@prisma/orm-extension-paradedb

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-extension-paradedb@31016

@prisma/orm-extension-pgvector

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-extension-pgvector@31016

@prisma/orm-extension-postgis

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-extension-postgis@31016

@prisma/orm-extension-supabase

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-extension-supabase@31016

@prisma/orm-family-mongo

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-family-mongo@31016

@prisma/orm-family-sql

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-family-sql@31016

@prisma/orm-framework

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-framework@31016

@prisma/orm-mariadb

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-mariadb@31016

@prisma/orm-mongo

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-mongo@31016

@prisma/orm-mysql

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-mysql@31016

@prisma/orm-postgres

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-postgres@31016

@prisma/orm-postgres-contrib

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-postgres-contrib@31016

@prisma/orm-sqlite

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-sqlite@31016

@prisma/orm-target-mariadb

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-target-mariadb@31016

@prisma/orm-target-mongo

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-target-mongo@31016

@prisma/orm-target-mysql

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-target-mysql@31016

@prisma/orm-target-postgres

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-target-postgres@31016

@prisma/orm-target-sqlite

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-target-sqlite@31016

@prisma/orm-toolchain

npm i https://pkg.pr.new/prisma/orm/@prisma/orm-toolchain@31016

commit: f781591

@SevInf
SevInf added this pull request to stack #31015 November 12, 2026 18:25
@SevInf
SevInf force-pushed the nested-mutation-many-to-many-writes branch from bee797c to 9a6b79f Compare November 12, 2026 18:45
@SevInf
SevInf force-pushed the nested-mutation-many-to-many-writes branch from 9a6b79f to a20fb3d Compare November 12, 2026 19:10
@SevInf
SevInf force-pushed the nested-mutation-many-to-many-writes branch from a20fb3d to 5142d53 Compare November 13, 2026 10:28
Base automatically changed from nested-mutation-filtered-writes to main November 13, 2026 11:41
The Postgres, SQLite and MySQL renderers accepted only one-column
subqueries. A ListExpression compared with IN to a subquery now renders
as a row value, and the subquery must project one column per value.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Steven McClankerton <[email protected]>
@SevInf
SevInf force-pushed the nested-mutation-many-to-many-writes branch from 5142d53 to 68df656 Compare November 13, 2026 11:41
SevInf and others added 3 commits November 13, 2026 14:05
…elation write the targets linked through the junction

The write on the target table keeps the caller filter and adds
key IN (SELECT link FROM junction), and the key of the parent goes into
that subquery: a FilterData condition lands in the SELECT whose FROM
alias its column names.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Steven McClankerton <[email protected]>
… no changes from extension authors

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Steven McClankerton <[email protected]>
…ST already expresses

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Steven McClankerton <[email protected]>
@SevInf
SevInf force-pushed the nested-mutation-many-to-many-writes branch from 68df656 to f781591 Compare November 13, 2026 14:06
@SevInf
SevInf added this pull request to the merge queue Nov 13, 2026
Merged via the queue into main with commit 02c69d7 Nov 13, 2026
25 checks passed
@SevInf
SevInf deleted the nested-mutation-many-to-many-writes branch November 13, 2026 15:52
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-prisma__orm that referenced this pull request Nov 13, 2026
…risma#31018)

Ports the upstream engine tests for nested `updateMany` and `deleteMany`
inside `update()`, which prisma#31013, prisma#31014 and prisma#31016 made possible. Fourth
part of slice `filtered-many-writes` of the nested-mutations project.
Stacked on prisma#31016. Tests and fixtures only.

Of the +5743 lines, 5150 are emitted fixtures (`contract.json`,
`contract.d.ts`); tests are +380, hand-written fixtures +175, ledgers
and docs +38.

## Changes

- **`nested_update_many_inside_update.rs` (8) and
`nested_delete_many_inside_update.rs` (6)**, ported into
`test/integration/test/ports/engines/writes/nested_mutations/already_converted/`,
all passing. Both suites share four fixtures under
`nested_mutations/_fixture/`, one per relation shape: one-to-one
optional, parent-to-many with a required child, parent-to-many with an
optional child, and many-to-many (junction keys cascade, as upstream's
implicit table does).
- **One variant per relation shape, by owner decision (2026-11-12).**
Upstream runs most of these tests over `relation_link_test`, a matrix of
up to 32 schema variants per relation shape (81 fixtures in prisma#30642).
Nested filtered writes do not depend on the shape of the key, and
composite keys are covered by prisma#31016's integration tests, so each test
is ported against one representative variant and the other variants are
recorded as not ported in
`engines/non-ported/writes/nested_mutations/already_converted/<suite>/<suite>.md`.
This is an exception to the matrix rule of
`projects/port-all-tests/spec.md`, recorded in the ports README.
- **Rejections on to-one relations** (`one2n_rel_error_nested_um`,
`o2n_rel_fail`) assert Prisma 8's `ORM.RELATION_MUTATION_UNSUPPORTED`
(`reason: 'to-one-relation'`); upstream answers with error 2009. The
checklist notes the difference.
- **`unchecked_nested_update_many.rs` ›
`disallow_write_parent_inline_rel_sclrs`** runs on an empty database and
asserts `ORM.RELATION_MUTATION_INVALID` (`problem:
'parent-link-column'`), with its own fixture (upstream's
`ModelA`/`ModelB` schema).
- **`combining_different_nested_mutations.rs` ›
`create_then_disconnect`** (taken out of prisma#31013), now on the shared
many-to-many fixture.
- `projects/port-all-tests/checklists/engines-writes.md`: the 16 entries
ticked.

## Verification

- The ported files on PGlite: 16 of 16 pass.
- `test/integration` typecheck and lint, `fixtures:check`, build,
`check:upgrade-coverage` against prisma#31016.

🤖 Generated with [Claude Code](https://claude.com/claude-code)


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Added integration coverage for nested child creation and
disconnection, filtered nested updates and deletes, and invalid nested
relation updates.
* **Documentation**
* Documented representative relation-shape coverage and listed nested
mutation variants that are not ported.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Steven McClankerton <[email protected]>
Co-authored-by: Steven McClankerton <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-prisma__orm that referenced this pull request Nov 13, 2026
…ed-row updateAll and deleteAll (prisma#31021)

Documents nested writes in the `sql-orm-client` README and the error
reference, including what slice `filtered-many-writes` of the
nested-mutations project added: the array return from a relation
callback (prisma#31013) and `updateAll`/`deleteAll` on related rows (prisma#31014,
prisma#31016). Last PR of the slice; stacked on prisma#31020. Docs only.

## Changes

- **`sql-orm-client` README, new "Nested writes" section:** the relation
callback and a table of each operation with where it is available and on
which relations; the many-to-many limits that already existed
(`disconnect()` without criteria, junction tables with required
columns); one transaction per multi-statement write; the array return
(order, empty array, at most one operation on a to-one relation, the
errors); `where(w).updateAll(data)`/`deleteAll()` and the forms without
`where`, touching only rows related to the record (by foreign key, or by
a junction row), with the filter forms `where` takes, the parent-link
rule and update defaults; and that many-to-many `deleteAll()` leaves
junction rows to the database's foreign-key action. No user doc under
`docs/` covered nested writes, so the README is where they are
described.
- **Error reference:** `ORM.RELATION_MUTATION_INVALID` and
`ORM.RELATION_MUTATION_UNSUPPORTED` list the new cases and the payload
fields the code sends (`index`, `problem: 'parent-link-column'`,
`fields`, `reason: 'to-one-relation'`, `operations`, `reason:
'junction-required-columns'`, `junction`).
- An `upgrade-instructions/pending/` entry declaring no changes, which
the coverage check requires for a README under `packages/3-extensions/`.

## Verification

`check:error-reference`, `check:upgrade-coverage` against prisma#31020,
`lint:docs`.

🤖 Generated with [Claude Code](https://claude.com/claude-code)


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **Documentation**
* Expanded guidance on nested relation writes, supported operations,
transaction behavior, and bulk updates and deletes.
* Clarified error reference details for invalid nested operations,
unsupported relation operations, and related error information.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Signed-off-by: Steven McClankerton <[email protected]>
Co-authored-by: Steven McClankerton <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

2 participants