You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
upsert() with MySQL mutates input entity objects in-place with wrong auto-increment IDs after ON DUPLICATE KEY UPDATE #12094
When calling upsert() on MySQL, TypeORM unconditionally writes back the auto-increment ID from insertResult.insertId into every input entity object — even when the rows were updated (not inserted). This silently corrupts the in-memory objects after the call returns.
Expected Behavior
Input objects passed to upsert() should not be mutated. If mutation is intentional and documented, the generated ID should only be applied to rows that were genuinely inserted (had no prior value for the PK), not to rows that were updated.
Actual Behavior
Any code that reads back from the input array after upsert() receives silently corrupted primary keys. A concrete example:
// Snapshot MUST be taken before upsert — taking it after produces wrong results:const deleteCriteria = entities.map(e => ({ employeeId: e.id })); // ← must be before upsertawait repo.upsert(entities, ["id"]);// entities[*].id are now wrong — deleteCriteria would target the wrong rows
The callee has no indication that mutation occurred. There is no warning, no error, and the upsert itself succeeds.
Steps to reproduce
Use upsert without spreading the entities and try to read the ID afterward.
My Environment
Dependency
Version
Operating System
Windows
Node.js version
v23.10.0
Typescript version
5.7.3
TypeORM version
0.3.25
Additional Context
MysqlDriver.createGeneratedMap() contains:
MySQL's ON DUPLICATE KEY UPDATE always returns a non-zero insertId — it is the auto-increment value that would have been assigned had this been a fresh insert. It is non-zero even when every row in the batch was an update. TypeORM has no mechanism to distinguish inserts from updates in the result, so it applies this ID to every entity regardless.
ReturningResultsEntityUpdator.insert() then calls:
...which overwrites the original object's primary key with the wrong value in-place.
Relevant Database Driver(s)
aurora-mysql
aurora-postgres
better-sqlite3
cockroachdb
cordova
expo
mongodb
mysql
nativescript
oracle
postgres
react-native
sap
spanner
sqlite
sqlite-abstract
sqljs
sqlserver
Probably everywhere I noticed it with MySQL.
Are you willing to resolve this issue by submitting a Pull Request?
Yes, I have the time, and I know how to start.
AI was used to write some parts of this, but I found and reviewed this as a legitimate user.
This was previously reported in #11083 (closed without a fix). A fix is now in progress in #12155, which addresses the input-entity mutation in ReturningResultsEntityUpdator.
Activity
pkuczynski commentedon Apr 15, 2026
This was previously reported in #11083 (closed without a fix). A fix is now in progress in #12155, which addresses the input-entity mutation in
ReturningResultsEntityUpdator.test(query-builder): type the upsert embedded-mutation input instead …