Skip to content

fix(core): match reloaded rows in upsertMany when inputs use different unique keys - #8396

Merged
B4nan merged 5 commits into
masterfrom
fix/upsert-many-mixed-unique-keys
Oct 5, 2026
Merged

B4nan merged 5 commits into
masterfrom
fix/upsert-many-mixed-unique-keys

Conversation

@B4nan

@B4nan B4nan commented Oct 5, 2026

Copy link
Copy Markdown
Member

On drivers without RETURNING, em.upsertMany() reloads the rows and matches each input to its row. Both the reload query and the matching assumed every input in the batch is identified by the same unique key as the first one. A batch where one row is identified by a unique code and another by a composite unique (or by the primary key) threw Cannot find matching entity for condition ....

The reload query now builds each $or branch from that row's own condition, and the reloaded rows are compared only on the keys of the condition being matched. The first part matters on its own: with the first row's key applied to all rows, a null in that column became code is null, which over-fetched and could hydrate an input from an unrelated row.

This does not change the ON CONFLICT target, which is still derived from the first row only. On SQLite and PostgreSQL a mixed batch therefore works for inserts, but hits a unique constraint violation when a row identified by a different key already exists.

…ent unique keys

Without RETURNING support, the reload query and the row matching used the unique key of the first input for the whole batch, and compared the reloaded rows on the keys of every condition at once. Inputs identified by another unique key (or by the primary key) failed with `Cannot find matching entity for condition`, or could be matched to an unrelated row.
@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 (1459d92) to head (52cc357).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8396      +/-   ##
==========================================
+ Coverage   96.60%   99.66%   +3.06%     
==========================================
  Files         293      293              
  Lines       32030    32040      +10     
  Branches     9197     8630     -567     
==========================================
+ Hits        30941    31934     +993     
+ Misses       1083      102     -981     
+ Partials        6        4       -2     

☔ 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.

…n `upsertMany`

Narrowing the reloaded row to the keys of the condition turned an empty condition into a match for every row. Compare the full row in that case, as before.
B4nan added 2 commits October 5, 2026 22:34
…mpty condition

Deriving the reload fields per row turned a row without any unique value into an empty `$or` branch, which matched every row and let the row be hydrated from an unrelated one.
@B4nan
B4nan merged commit 20c571d into master Oct 5, 2026
40 of 49 checks passed
@B4nan
B4nan deleted the fix/upsert-many-mixed-unique-keys branch October 5, 2026 21:34
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