Skip to content

fix(core): upsert rows with a null unique key without touching unrelated rows - #8397

Merged
B4nan merged 13 commits into
masterfrom
fix/upsert-many-null-unique-key
Oct 6, 2026
Merged

B4nan merged 13 commits into
masterfrom
fix/upsert-many-null-unique-key

Conversation

@B4nan

@B4nan B4nan commented Oct 5, 2026 •

Copy link
Copy Markdown
Member

em.upsertMany() threw Cannot find matching entity for condition {} when a row had null in its unique key and the batch had to be reloaded to get the primary keys: always on MySQL/MariaDB (no RETURNING), and on the other SQL drivers with onConflictAction: 'ignore' or onConflictWhere. When every row had a null key, it could instead return entities hydrated from an unrelated row. Reported in the review of #8393.

A null unique 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 through em.upsert(), which reads the primary key from the insert result. That costs one insert and one select per null-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 explicit onConflictFields when 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 RETURNING or OUTPUT the 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 through em.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.

…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

codecov Bot commented Oct 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.66%. Comparing base (20c571d) to head (b5a4923).
⚠️ Report is 7 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

B4nan added 2 commits October 5, 2026 20:59
…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.
B4nan added 3 commits October 5, 2026 21:37
… 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.
@B4nan B4nan changed the title fix(core): load primary keys of rows with a null unique key in upsertMany fix(core): identify upserted rows with a null unique key by their primary key Oct 5, 2026
B4nan added 6 commits October 5, 2026 21:49
… 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.
…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.
@B4nan B4nan changed the title fix(core): identify upserted rows with a null unique key by their primary key fix(core): upsert rows with a null unique key without touching unrelated rows Oct 6, 2026
@B4nan
B4nan merged commit bb4aa99 into master Oct 6, 2026
26 checks passed
@B4nan
B4nan deleted the fix/upsert-many-null-unique-key branch October 6, 2026 15:06
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.

1 participant