typeorm: Cannot update entity because entity id is not set in the entity.
Issue Description
When trying to save a record I get Cannot update entity because entity id is not set in the entity.
The payload do not contain any id.
Expected Behavior
Being able to create a new record.
Actual Behavior
Throws the error 'Cannot update entity because entity id is not set in the entity.'
Steps to Reproduce
- Get a transaction,
- Run save method on your entity with said EntityManager and a payload without “id” property.
return this.repo.manager.transaction(entityManager) => {
return entityManager.save(Entity, payload);
})
My Environment
| Dependency | Version |
|---|---|
| Operating System | MacOs 11.2.3 |
| Node.js version | v14.16.1 and v12.18.4 |
| Typescript version | typescript@4.2.4 |
| TypeORM version | typeorm@0.2.32 |
Additional Context
using nest.js: @nestjs/cli@7.6.0 typescript@4.2.3
Relevant Database Driver(s)
-
aurora-data-api -
aurora-data-api-pg -
better-sqlite3 -
cockroachdb -
cordova -
expo -
mongodb -
mysql -
nativescript -
oracle -
postgres -
react-native -
sap -
sqlite -
sqlite-abstract -
sqljs -
sqlserver
Are you willing to resolve this issue by submitting a Pull Request?
- Yes, I have the time, and I know how to start.
- Yes, I have the time, but I don’t know how to start. I would need guidance.
- No, I don’t have the time, although I believe I could do it if I had the time…
- No, I don’t have the time and I wouldn’t even know how to start.
Thanks in advance for any help !
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 22 (3 by maintainers)
Commits related to this issue
- fix: workaround for typeorm save bug https://github.com/typeorm/typeorm/issues/7643#issuecomment-1081398542 — committed to dehy/foodcoop-mobile-app by dehy 2 years ago
For me it was because I wasn’t using
@PrimaryGeneratedColumn() in the entity definition
I don’t know why?
I set primary key
id = nullbefore.save()and there is no error。@SFantasy yeah it resolved itself, i guess we can close the issue
I found an answer in the thread here https://github.com/typeorm/typeorm/issues/4651
You want to set the
reloadoption in the argument to save’s options argument tofalseentity.id = nullbefore saving, you’re the man 🚀 It works for me too.Still needs a proper fix!
same problem, if don’t use default property, it’s fine
unfortunately this solution will keep generating new records on upsert, instead of updating the current one, when the constraint is not the primary key.
This is caused when the table primary key (id) does not have Auto Increment set or marked, In that case you will have to be inserting the primary key by yourself
yeah but as @jiayisheji mentioned here, if I set
idtonull, the primary key (id) will be increased. Anyone have solutions for this ?https://github.com/typeorm/typeorm/issues/7643#issuecomment-1086984078
@jiayisheji check your sql mode
NO_AUTO_VALUE_ON_ZERO affects handling of AUTO_INCREMENT columns. Normally, you generate the next sequence number for the column by inserting either NULL or 0 into it. NO_AUTO_VALUE_ON_ZERO suppresses this behavior for 0 so that only NULL generates the next sequence number.