Repository navigation
fix(query-builder): prevent orIgnore returning mismatch on skipped entities - #13051
Asadshah7950 wants to merge 5 commits into
Conversation
…tities (typeorm#12994) When performing bulk inserts with .insert().orIgnore() on drivers supporting RETURNING statements, databases skip writing rows that violate unique constraints and return only the rows that were actually inserted. Previously, ReturningResultsEntityUpdator mapped insertResult.raw[entityIndex] strictly by positional array index, which shifted returned identifiers and autogenerated columns onto skipped entities and left subsequent inserted entities undefined. This commit resolves the issue by: 1. Ensuring InsertQueryBuilder includes inserted columns in extraReturningColumns when onIgnore is enabled on drivers supporting RETURNING, so returned rows contain identifying column values. 2. Correlating returned raw rows to entities in ReturningResultsEntityUpdator by primary key or inserted column values when onIgnore is enabled or fewer rows are returned than entities. 3. Leaving skipped entities untouched and assigning undefined to their corresponding positions in insertResult.identifiers and insertResult.generatedMaps.
Code Review by Qodo
1. Ignored inserts can fail on transformed values
|
Addressed Qodo Review Edge Cases (commit 8692f73)Thank you @qodo-cover-pr-agent for the thorough automated review. All 5 edge cases identified in the review have been addressed:
Unit tests covering all 5 scenarios have been added to est/github-issues/12994/issue-12994.test.ts. All 31 existing query builder insert tests and 10 issue tests pass cleanly. |
Thanks for the update. The published report already marks all five findings as resolved in commit
I haven't checked the new tests or the code itself. If you want Qodo to re-review the PR against the latest commit, ask me to run a review. |
…atching signatures
Addressed Qodo Review Finding (commit dba7220)Thank you @qodo-free-for-open-source-projects for the sharp review catch. The candidate verification and signature delimiter boundary issue has been resolved:
|
Thanks for the update. The fix you describe targets finding 1 (skipped rows receiving another row's ID), but the published report still lists it as open. That report covers commit 8692f73, and I can't see a review of dba7220 yet, so the report hasn't caught up.
If you want the report refreshed against dba7220, ask me to run a review again. |
…nd move tests to functional
Addressed All Updated Qodo Review Findings & Compliance Rules (commit
|
I can't start a new review from this chat, because the review-rerun tool isn't available on this deployment. The current report has no open findings: Here's what I can do:
To get a fresh review of commit |
…comments/targets, and document InsertResult
Addressed Remaining Qodo Findings & Compliance Rules (commit
|
Description of change
Fixes an issue where \insert().orIgnore()\ (or \ON CONFLICT DO NOTHING) shifts database-returned rows onto the wrong entity instances when one or more rows are skipped due to a unique constraint conflict (Closes #12994, Fixes #5226).
Root Cause
When executing multi-row inserts with .orIgnore()\ on database drivers supporting \RETURNING\ clauses (such as PostgreSQL, CockroachDB, MSSQL), the database skips conflicted rows and returns only the rows that were actually inserted (fewer rows than the original \entities\ array).
Previously, \ReturningResultsEntityUpdator.insert()\ assumed a 1:1 positional index mapping:
\\ s
const result = Array.isArray(insertResult.raw)
? insertResult.raw[entityIndex]
: insertResult.raw
\
When an entity at index \