Repository navigation
fix(core): upsert rows with a null unique key without touching unrelated rows - #8397
Merged
Merged
Conversation
…Many A null unique value never conflicts, so the row is always inserted, but it cannot be reloaded by that value. When the returned rows cannot be mapped by position (no RETURNING support, onConflictAction ignore, onConflictWhere), such rows go through em.upsert(), which maps the primary key from the insert result. em.upsert() falls back to the primary key when the reload condition would be empty.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #8397 +/- ##
========================================
Coverage 99.66% 99.66%
========================================
Files 293 293
Lines 32040 32070 +30
Branches 9024 9221 +197
========================================
+ Hits 31934 31964 +30
Misses 102 102
Partials 4 4 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…or null unique keys MongoDB inserts rows with an empty condition as part of the batch, while a single row upsert with an empty condition would match every document. The em.upsert() reload fallback now covers composite primary keys too.
When neither the primary key nor a non-null unique value of the upserted row is known, the reload after em.upsert() ran with an empty condition and hydrated the entity from an arbitrary row. The SQL drivers now throw instead.
This was referenced Oct 5, 2026
… upserted rows upsertMany reloaded rows with a partial composite PK or a database generated PK by a condition matching unrelated rows. Such rows now go through the single row path, which returns the generated PK when the driver supports RETURNING, and throws otherwise.
The mongo driver no longer reloads an upsert with an empty condition, so the returned primary key does not need to be limited to RETURNING platforms.
… primary key Rows with an empty condition that stay in the upsertMany batch (autoincrement PK without a unique key, mongo) were reloaded by the unique fields of the first row, or by an empty condition, and hydrated from an unrelated row. They are now matched by the primary key the insert reported.
… an unrelated row
…unique value The parent table reports a single insertId for the batch, so a second such row would get the same primary key. The entity manager sends those rows one by one, the guard covers direct driver calls.
…ertMany row matching
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.
em.upsertMany()threwCannot find matching entity for condition {}when a row hadnullin its unique key and the batch had to be reloaded to get the primary keys: always on MySQL/MariaDB (noRETURNING), and on the other SQL drivers withonConflictAction: 'ignore'oronConflictWhere. When every row had anullkey, it could instead return entities hydrated from an unrelated row. Reported in the review of #8393.A
nullunique value never conflicts, so the row is always inserted, but there is nothing to find it by afterwards. When the returned rows cannot be mapped by position, such rows now go throughem.upsert(), which reads the primary key from the insert result. That costs one insert and one select pernull-key row on those paths. Entities with an autoincrement PK and no unique key keep the single batch statement, and so does MongoDB, which already inserts such rows as part of the batch. The rows that stay in the batch are reloaded by the primary key the insert reported, not by the unique fields of the first row.em.upsert()had the same problem with explicitonConflictFieldswhen the row had to be reloaded: the reload ran with an empty condition and hydrated the entity from an arbitrary row. It now falls back to the primary key.Two more shapes ended up on an unrelated row in both methods: a composite PK that is only partially in the data, and a PK generated by a database default (not autoincrement) that is missing in the data. A PK missing in the data is now part of the returned columns, so with
RETURNINGorOUTPUTthe entity gets the row that was written.TPT children were affected as well. The child table targeted the parent row found by the (null) unique key, which was an unrelated row. The SQL driver now takes the PK of such a row from the parent insert and reports it, and
em.upsertMany()sends those rows throughem.upsert()one by one, since a child table cannot return its rows. A direct driver call with more than one such row in a batch is refused.One behaviour change: when there is nothing to find the row by (no PK, no non-null unique value, no
RETURNING), the SQL drivers now throw instead of hydrating the entity from an unrelated row. The statement has already run by then, so the row is written unless the call is inside a transaction.