Description
Issue description
Using queryRunner.manager.upsert() on tables whose id's type is number will result in error: Cannot update entity because entity id is not set in the entity.
Expected Behavior
The upsert runs successfully.
Actual Behavior
Error thrown: Cannot update entity because entity id is not set in the entity.
, in ReturningResultsEntityUpdator.insert()
const entityIds = entities.map((entity) => {
const entityId = metadata.getEntityIdMap(entity);
// We have to check for an empty `entityId` - if we don't, the query against the database
// effectively drops the `where` clause entirely and the first record will be returned -
// not what we want at all.
if (!entityId) {
throw new error_1.TypeORMError(`Cannot update entity because entity id is not set in the entity.`);
}
return entityId;
});
The upsert should run properly if the id's type is bigint, and fail with other types, since in ColumnMetadata.createValueMap(), it converts a field value to string if
this.generationStrategy === "increment" ||
this.generationStrategy === "rowid") &&
this.type === "bigint" &&
value !== null
The process update instead of inserting the record, so insert_id
will be undefined. Hence, the upsert will not throw the aforementioned error if the id is of bigint type, as the id will appear as "undefined"
.
Steps to reproduce
Create a table Test
with id as follow:
@PrimaryGeneratedColumn("increment")
id: number;
and a unique pair of keys
@Index(["key1", "key2"], { unique: true })
...
@Column()
key1: string;
@Column()
key2: string;
finally a value field for upserting:
...
@column()
value: string;
Then just run `queryRunner.manager.upsert(Test, {key1: "a", key2: "b", value, "c"}, ["key1", "key2"])` (run 2 times if the record isn't in the table)
### My Environment
| Dependency | Version |
| --- | --- |
| Operating System | Windows |
| Node.js version |22.15.0 |
| Typescript version | 5.7.2 |
| TypeORM version | 0.3.20 |
### Additional Context
_No response_
### Relevant Database Driver(s)
- [ ] aurora-mysql
- [ ] aurora-postgres
- [ ] better-sqlite3
- [ ] cockroachdb
- [ ] cordova
- [ ] expo
- [ ] mongodb
- [x] mysql
- [ ] nativescript
- [ ] oracle
- [ ] postgres
- [ ] react-native
- [ ] sap
- [ ] spanner
- [ ] sqlite
- [ ] sqlite-abstract
- [ ] sqljs
- [ ] sqlserver
### Are you willing to resolve this issue by submitting a Pull Request?
No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.