Repository navigation
docs(projects): shape the createAll conflict-skip project - #30363
Closed
wmadden-electric wants to merge 2 commits into
Closed
wmadden-electric wants to merge 2 commits into
wmadden-electric wants to merge 2 commits into
Conversation
Spec, design notes, and plan for an onConflict: "skip" option on createAll and createAndCount, capability-gated for Postgres and SQLite, with the createAndCount affected-row count fix as the first slice. Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Co-Authored-By: Claude Fable 5.1 <[email protected]> Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]>
Contributor
|
Important Review skippedReview was skipped due to path filters ⛔ Files ignored due to path filters (4)
CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including ⚙️ Run configurationConfiguration used: Repository: prisma/orm/.coderabbit.yml Review profile: CHILL Plan: Advanced Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Comment |
This was referenced Sep 21, 2026
Contributor
Author
|
Folded into #30365 as a single PR. |
wmadden-electric
added a commit
that referenced
this pull request
Sep 22, 2026
…que constraint (#30365) ```ts const rows = [ { id: 1, email: '[email protected]' }, { id: 2, email: '[email protected]' }, // same email as row 1 { id: 3, email: '[email protected]' }, ]; const inserted = await db.User.createAll(rows, { onConflict: 'skip' }); // [{ id: 1, ... }, { id: 3, ... }] row 2 was skipped by the database const count = await db.User.createAndCount(rows, { onConflict: 'skip' }); // 2 ``` Before this PR, that call threw a unique violation and inserted nothing. Now the database skips the colliding row and the ORM returns only what was actually written. ## The decision `createAll` and `createAndCount` accept an options object. Its only option is `onConflict: 'skip'`, optionally with `conflictOn: ['email']` to name which unique constraint counts as "a conflict". The SQL is `INSERT ... ON CONFLICT DO NOTHING`, or `ON CONFLICT ("email") DO NOTHING` when a target is named. Prisma 7 called this `createMany({ skipDuplicates: true })`. We promised a user in `#prisma-8` on 2026-09-09 that Prisma 8 would get an equivalent. The scorecard listed it as deliberately absent, which contradicted that promise. ## How it works, from the SQL up **The SQL layer already knew how to do this, almost.** `upsert` uses `ON CONFLICT` today, so the SQL AST has an `InsertOnConflict` node and both renderers emit the clause. The one restriction was that a conflict clause had to name at least one column. Postgres and SQLite both accept `ON CONFLICT DO NOTHING` with no target, meaning "skip on any unique constraint", and that is what `skipDuplicates` did. So the AST gains a targetless form that is valid only with `DO NOTHING`. A targetless `DO UPDATE SET` makes no sense and both renderers still reject it. **What comes back.** `createAll` uses `RETURNING`, so it naturally yields only the rows the database inserted. `createAndCount` was a different story: it returned the length of the input array, not anything the database said. That was already a bug (a trigger could drop a row and the count would lie), and it would have made the count wrong for every skipped row. It now reads the driver's affected-row count, the way `updateAndCount` and `deleteAndCount` always have. That fix is the second commit and stands on its own. **Why SQLite needs a little more.** SQLite cannot take `DEFAULT` as a value in a multi-row insert, so when rows have different column sets the ORM already splits one `createAll` into one statement per column signature. The conflict clause goes on every one of those statements, and `createAndCount` sums the counts. **Why this is behind capabilities.** Prisma 8 never asks "which database is this". Instead, adapters report what they can do, the emitted `contract.json` records those capabilities, and features check for a capability key at the point of use. Two new keys: - `sql.insertOnConflictSkip`: the adapter can skip rows on a unique conflict at all. - `sql.insertOnConflictWithoutTarget`: it can do so without naming the constraint. Postgres and SQLite report both. The split exists for targets we plan to add. MySQL has no `ON CONFLICT` but can skip with `INSERT IGNORE`. SQL Server has neither and can only express "skip" as a `MERGE` or `WHERE NOT EXISTS` on an explicit key, so it will report the first key and not the second. Without the first key the option is refused with `ORM.CAPABILITY_MISSING`; without the second, only the untargeted form is refused. That is also why `conflictOn` is part of the API from day one rather than something bolted on when SQL Server arrives. **What is refused on purpose.** Multi-table inheritance variants: a variant row is a base-table insert followed by a variant-table insert per row, and a skipped variant insert would leave an orphan base row. Refused with `ORM.OPERATION_UNSUPPORTED` until we do savepoint-per-row. `create()` does not take the option; a single row that may or may not be inserted is `upsert` with an empty update, which already exists. Mongo is untouched. ## What is in the diff - `packages/2-sql/4-lanes/relational-core`: `InsertOnConflict.doNothing()` for the targetless form. - `packages/3-targets/6-adapters/{postgres,sqlite}`: render the targetless clause; report the two capability keys in the runtime profile and in the map that the emitter copies into `contract.json`. - `packages/3-extensions/sql-orm-client`: the option on `createAll` and `createAndCount`, the capability and MTI checks, `conflictOn` field-to-column mapping through the same path `upsert` uses, and the `createAndCount` count fix. - `docs/reference/capabilities.md`, the sql-orm-client README, JSDoc on both methods, the scorecard row, and an upgrade note. - `projects/createall-conflict-skip/`: the spec, design notes, and plan this was built from. Transient; deleted at close-out per the projects convention. - 473 regenerated `contract.json` / `contract.d.ts` fixtures. See the next section before you scroll through them. ## About the regenerated contracts Every emitted contract carries the adapter's capability map, so adding two keys touches every fixture in the repo. I checked all 473 files mechanically: the only change in any of them is the two added keys, and no hash moved. Eighteen of them are migration snapshots under `migrations/snapshots/`. Our convention says snapshots are written once, but the regen scripts that `pnpm fixtures:check` runs rewrite snapshot contracts unconditionally, so leaving them out fails the check. They are included; a contract without the keys still loads fine and simply refuses the option. Making the regen scripts leave existing snapshots alone is a separate follow-up. Users on an existing contract must re-emit it before they can use the option. The upgrade note under `upgrade-instructions/pending/` says so. ## How it is tested - Postgres integration, `test/integration/test/sql-orm-client/create-on-conflict.test.ts`: collision against an existing row, targeted and untargeted, every row colliding, a collision on a constraint other than the named one still raising, both capability refusals, and a batch whose rows collide with each other. Each goes red if the clause is dropped. - SQLite end to end: the split path with a collision in one of two statements, and the within-batch case. - Two Prisma engine cases (`create_many_no_error_skip_dup` and its `createManyAndReturn` variant) ported faithfully under `test/integration/test/ports/engines/writes/top_level_mutations/create_many/` and ticked in the port checklist. - Unit tier: the AST node, both renderers with exact SQL strings, plan compilation, collection behaviour including the old callback-in-second-position call form, and every structured error code. - The `createAndCount` fix: a `BEFORE INSERT` trigger drops one of three rows on Postgres and the count comes back as 2. ## Alternatives considered - **`skipDuplicates: true`, the Prisma 7 name.** Familiar, but a boolean cannot carry a conflict target, and SQL Server needs one. Rejected. - **`onConflict: 'doNothing'`.** Serhii's suggestion; matches the SQL. Kept `'skip'` because it is target-neutral: MySQL spells this `INSERT IGNORE` and SQL Server `MERGE`, and `'skip'` names what the caller observes. Neither of us felt strongly. - **Letting `onConflict` grow an `update` value later.** No. `upsert` owns update-on-conflict; a batch form would be its own method. - **A separate method such as `createAllIgnoringConflicts`.** Doubles with `createAndCount`. Rejected. - **A chained modifier, `db.User.onConflict('skip').createAll(rows)`.** Puts a write-only flag into builder state that `find`, `select`, and `include` share. Rejected. - **Always requiring `conflictOn`.** Most portable, but forces every Postgres and SQLite user to name a constraint for the common case. The second capability key makes the requirement target-specific instead. - **Supporting MTI variants with a savepoint per row.** Correct but a second transaction concern inside an experimental surface. Deferred. Supersedes #30363 and #30364, which were these commits split for stacked review. No Linear issue exists for this work yet. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-authored-by: Claude Fable 5.1 <[email protected]>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Decision
Ship an
onConflict: 'skip'option oncreateAllandcreateAndCountin the SQL ORM client, compiling toON CONFLICT DO NOTHING, gated on two adapter-reported capabilities so MySQL and SQL Server can implement it later with an explicit conflict target. FixcreateAndCountfirst, because it returnsdata.lengthinstead of the database's count.Why now
Will promised this to a user in
#prisma-8on 2026-09-09 ("skipDuplicates is on our list, I'll let you know once it's available"). The scorecard listscreateMany({ skipDuplicates })as deliberately absent, which contradicts that promise.What this PR contains
Shaping artifacts only, under
projects/createall-conflict-skip/: the project spec, design notes with the alternatives considered, the two-slice plan, and the slice 1 spec. No code changes. Serhii reviewed the API naming;'skip'stays, and the option never gains an update value becauseupsertowns that.Not yet done
No Linear Project exists for this yet, so the tracker links in the spec are placeholders.
🤖 Generated with Claude Code