Repository navigation
fix(query-builder): map orIgnore/orUpdate returning rows to entities by key - #13001
ElwinErnst wants to merge 3 commits into
Conversation
Code Review by Qodo
1. Inserted rows can be assigned to skipped entities
|
ed7156f to
a48de09
Compare
…by key ReturningResultsEntityUpdator.insert matched RETURNING rows to entities by array position. With ON CONFLICT DO NOTHING (orIgnore) or a DO UPDATE that skips a row, RETURNING comes back shorter than the entity list, so every row after a skipped one shifted onto the previous entity: a skipped entity received the next inserted row's id (and other returned columns), and the inserted entity lost its generated values. With user-set ids two entities could even end up sharing one id. Match returned rows to entities by the columns that identify the conflicting row (primary and unique columns) instead of by position. Those columns are added to RETURNING for the on-conflict-do-update drivers, and the rows, which come back in entity order, are consumed in order so each is assigned only to the entity whose identifying values equal it. Skipped entities are left untouched. When no row is skipped the previous positional path is kept unchanged. Verified on PostgreSQL across skip-first, skip-middle, skip-last, multi-skip and no-skip cases; regression tests added to the insert on-conflict functional suite (postgres, cockroachdb, better-sqlite3). Closes typeorm#12994
a48de09 to
2a09e17
Compare
|
Thanks for the review. Addressed the test-placement note: the regression coverage now lives in the existing |
commit: |
Harden the conflict-row-to-entity matching added for orIgnore/orUpdate: - Fall back to positional mapping when identity matching cannot account for every returned row (previously a batch where no entity carried a pre-insert identifying value, or a mismatch broke the walk, would discard generated values for genuinely-inserted rows — worse than the positional behaviour it replaced). - Hydrate the raw RETURNING value via the driver before comparing it to the entity value, and reuse ColumnMetadata.compareEntityValue, so transformer columns, booleans stored as 0/1 and date representations no longer produce false negatives that drop or misassign generated ids. Adds regression coverage with a transformer-backed unique column.
… in orIgnore mapping
entityMatchesReturnedRow fell back to String() equality, so an entity whose
identifying value is the literal string "null" matched a returned row whose
value is a real SQL NULL (String(null) === String("null")). A skipped entity
keyed on "null" could then consume the returned row of an inserted entity
keyed on NULL and receive the wrong generated values.
Reject null-versus-non-null comparisons before the String() fallback. Add a
regression to the insert on-conflict suite covering an ignored "null" key
followed by an inserted NULL key.
|
Thanks for the review. Pushed Fixed
Keeping as-is, with rationale
|
Description of change
insert().orIgnore()(andorUpdate()paths that skip a row) wrote theRETURNINGrows onto the wrong entities.Current behavior
ReturningResultsEntityUpdator.insertmatched returned rows to entities by array position (insertResult.raw[entityIndex]). WithON CONFLICT ... DO NOTHING, a skipped row returns noRETURNINGrow, so every row after the skipped one shifts up onto the previous entity:.returning()columns);This silently breaks the common "insert, ignore duplicates, then act on what was inserted" pattern. Same underlying bug as #5226.
New behavior
Returned rows are matched to the entities that produced them by the columns that identify the conflicting row (primary + unique columns) instead of by position:
EntityMetadata.getUpsertConflictIdentificationColumns()returns the primary and unique-constraint/unique-index columns;on-conflict-do-updatedrivers those columns are added toRETURNINGwhenorIgnore/orUpdateis used;When no row is skipped, the previous positional mapping is kept unchanged.
How it was verified
Reproduced the issue on PostgreSQL, then confirmed the fix across skip-first, skip-middle, skip-last, multi-skip and no-skip cases. New regression tests in
test/github-issues/12994/run onpostgres,cockroachdbandbetter-sqlite3. Existing on-conflict and a broad set of insert/persistence tests pass unchanged on PostgreSQL.Pull-Request Checklist
masterbranchFixes #00000tests/**.test.ts)docs/docs/**.md) — N/A (bug fix, no public API change)Fixes #12994