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
Cannot update entity because entity id is not set in the entity. #7643
When opening an issue, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This boils down to ensuring your code that reproduces the problem follows the following guidelines:
Minimal – Use as little code as possible that still produces the same problem
Complete – Provide all parts someone else needs to reproduce your problem in the question itself
Reproducible – Test the code you're about to provide to make sure it reproduces the problem
Minimal
The more code there is to go through, the less likely people can find your problem. Streamline your example in one of two ways:
Restart from scratch. Create a new program, adding in only what is needed to see the problem. Use simple, descriptive names for functions and variables – don’t copy the names you’re using in your existing code.
Divide and conquer. If you’re not sure what the source of the problem is, start removing code a bit at a time until the problem disappears – then add the last part back.
Don't sacrifice clarity for brevity when creating a minimal example. Use consistent naming and indentation, and include code comments if needed. Use your code editor’s shortcut for formatting code.
Don't include any passwords or credentials that must be kept secret.
Complete
Make sure all information necessary to reproduce the problem is included in the issue itself.
If the problem requires some code as well as some XML-based configuration, include code for both. The problem might not be in the code that you think it is in.
Use individual code blocks for each file or snippet you include. Provide a description for the purpose of each block.
DO NOT use images of code. Copy the actual text from your code editor, paste it into the issus, then format it as code. This helps others more easily read and test your code.
Reproducible
To help you solve your problem, others will need to verify that it exists.
Describe the problem. "It doesn't work" isn't descriptive enough to help people understand your problem. Instead, tell other readers what the expected behavior should be. Tell other readers what the exact wording of the error message is, and which line of code is producing it. Use a brief but descriptive summary of your problem as the title of your question.
Eliminate any issues that aren't relevant to the problem. If your question isn’t about a compiler error, ensure that there are no compile-time errors.
Double-check that your example reproduces the problem! If you inadvertently fixed the problem while composing the example but didn't test it again, you'd want to know that before asking someone else to help.
@SFantasy yeah it resolved itself, i guess we can close the issue
Reacted by Brendo Vale, Riedl Kevin, Bsc., Arnaud de Mouhy, qbyte2, adnanaslam475, mgasparyan, Dan, Number16BusShelter, Jonathan Luna, koma4028 and 11 more
For me it was because I wasn't using
@PrimaryGeneratedColumn() in the entity definition
Reacted by Peng Qi, Yui Sakamoto, Dmitriy Pushkov, Camillo Rinaldi, Evin Dunn, ao-si-luo-ma-li, Lautaro Nahuel González, danyflorez, Jerome, Julien T and 3 more
You want to set the reload option in the argument to save's options argument to false
Thanks for the info! But why should we have to set this option? On 0.2.34, it's working fine without this option, on 0.2.35+, I have to set it. What happened?
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.
if(){}else{if(){}else{// If not set null ,else undefinedif(entity[this.propertyName]!==undefined&&(returnNulls===false||entity[this.propertyName]!==null))return{[this.propertyName]: entity[this.propertyName]};returnundefined;}}
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
I set primary key id = null before .save() and there is no error。
unfortunately this solution will keep generating new records on upsert, instead of updating the current one, when the constraint is not the primary key.
Reacted by slider, Lea Waller, moussetomo and Karen Torres
I was having this error on an entity with a compound primary key. In our case, the problem was that one of the properties was not being automatically filled.
If you're getting this error, it's probably because your primary key, or part of your primary key is not being populated for whatever reason. You either need to provide a value in code, or use a default value the DB can fill in. In our case, unix_timestamp doesn't work as a default value so we use a @BeforeInsert() to fill in the date columns tagged with @Createdate().
You have to set the generation strategy for your primary key when creating the table
Like this
```
name: "id",
type: "integer",
isPrimary: true,
isGenerated:true,
generationStrategy: 'increment'
Activity
ganshiqingyuan commentedon May 28, 2021
seems like not set primaryColumns in entity
Sufiane commentedon May 28, 2021
it is set, that was the strangest part
binvb commentedon May 31, 2021
same problem, if don't use default property, it's fine
imnotjames commentedon Jun 21, 2021
When opening an issue, people will be better able to provide help if you provide code that they can easily understand and use to reproduce the problem. This boils down to ensuring your code that reproduces the problem follows the following guidelines:
Minimal
The more code there is to go through, the less likely people can find your problem. Streamline your example in one of two ways:
Don't sacrifice clarity for brevity when creating a minimal example. Use consistent naming and indentation, and include code comments if needed. Use your code editor’s shortcut for formatting code.
Don't include any passwords or credentials that must be kept secret.
Complete
Make sure all information necessary to reproduce the problem is included in the issue itself.
If the problem requires some code as well as some XML-based configuration, include code for both. The problem might not be in the code that you think it is in.
Use individual code blocks for each file or snippet you include. Provide a description for the purpose of each block.
DO NOT use images of code. Copy the actual text from your code editor, paste it into the issus, then format it as code. This helps others more easily read and test your code.
Reproducible
To help you solve your problem, others will need to verify that it exists.
Describe the problem. "It doesn't work" isn't descriptive enough to help people understand your problem. Instead, tell other readers what the expected behavior should be. Tell other readers what the exact wording of the error message is, and which line of code is producing it. Use a brief but descriptive summary of your problem as the title of your question.
Eliminate any issues that aren't relevant to the problem. If your question isn’t about a compiler error, ensure that there are no compile-time errors.
Double-check that your example reproduces the problem! If you inadvertently fixed the problem while composing the example but didn't test it again, you'd want to know that before asking someone else to help.
SFantasy commentedon Jun 25, 2021
@Sufiane it this problem solved ?
Sufiane commentedon Jun 25, 2021
@SFantasy yeah it resolved itself, i guess we can close the issue
danieloi commentedon Sep 2, 2021
I still have the same issue. How did you resolve it @Sufiane?
Sufiane commentedon Sep 2, 2021
to be honest it went by itself, i destroyed the dist multiple times restarted my app.
Are you using class-transformer/validator maybe ?
danieloi commentedon Sep 2, 2021
I'm not.
Thanks for responding
danieloi commentedon Sep 2, 2021
I found an answer in the thread here
#4651
You want to set the
reloadoption in the argument to save's options argument tofalseerald14 commentedon Sep 3, 2021
For me it was because I wasn't using
@PrimaryGeneratedColumn() in the entity definition
dehy commentedon Nov 29, 2021
Thanks for the info! But why should we have to set this option? On 0.2.34, it's working fine without this option, on 0.2.35+, I have to set it. What happened?
davidarias commentedon Dec 1, 2021
I have the same issue updating from 0.2.34 to latest ( 0.2.41 ).
Saving using the
reloadflag tofalsefixed the problem, but as dehy says, I'm wondering why this change is needed.jiayisheji commentedon Mar 29, 2022
I don't know why?
I set primary key
id = nullbefore.save()and there is no error。ntedgi commentedon Apr 3, 2022
@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.
jiayisheji commentedon Apr 4, 2022
@ntedgi Fix Typeorm bug not mysql error.
https://github.com/typeorm/typeorm/blob/master/src/metadata/ColumnMetadata.ts#ColumnMetadata.getEntityValueMap() :
not set null insert:
mysql insert
id = 0;set null insert:
mysql insert
id = 1++;I don't know why, I set null to fix Typeorm bugs and mysql insert issues.
dehy commentedon Apr 7, 2022
entity.id = nullbefore saving, you're the man 🚀 It works for me too.Still needs a proper fix!
fix: workaround for typeorm save bug
TranBaVinhSon commentedon May 7, 2022
yeah but as @jiayisheji mentioned here, if I set
idtonull, the primary key (id) will be increased. Anyone have solutions for this ?#7643 (comment)
Saintiano commentedon Feb 11, 2023
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
ktamas77 commentedon Mar 19, 2023
unfortunately this solution will keep generating new records on upsert, instead of updating the current one, when the constraint is not the primary key.
nhusby commentedon Aug 30, 2023
I was having this error on an entity with a compound primary key. In our case, the problem was that one of the properties was not being automatically filled.
The solution was to use our custom date decorator (we are using unix_timestamp integer values.)
If you're getting this error, it's probably because your primary key, or part of your primary key is not being populated for whatever reason. You either need to provide a value in code, or use a default value the DB can fill in. In our case, unix_timestamp doesn't work as a default value so we use a @BeforeInsert() to fill in the date columns tagged with @Createdate().
ricardocosta43 commentedon Sep 9, 2023
It's not my case, the key in the table was not auto-incrementing

humayyun00 commentedon Jul 18, 2024
You have to set the generation strategy for your primary key when creating the table
Like this
```
name: "id",
type: "integer",
isPrimary: true,
isGenerated:true,
generationStrategy: 'increment'
danielps-fideclub commentedon Jan 9, 2025
i got the same issue and this was the answer after some testing. i added .updateEntity(false). I still dont have all teh details associated to this behaviour.
https://stackoverflow.com/questions/74149526/typeorm-cannot-update-entity-because-entity-id-is-not-set-in-the-entity