Cannot update entity because entity id is not set in the entity.

See original GitHub issue

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

  1. Get a transaction,
  2. 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 !

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
erald14commented, Sep 3, 2021

For me it was because I wasn’t using
@PrimaryGeneratedColumn() in the entity definition

7reactions
jiayishejicommented, Mar 29, 2022

I don’t know why?

I set primary key id = null before .save() and there is no error。

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typeorm - Cannot update entity because entity id is not set in ...
TypeORM tracks entities with meta-data and most likely the id is still in the meta-data or has gotten in the meta-data.
Read more >
typeorm cannot read properties of undefined - You.com
TypeORM: Cannot read property 'id' of undefined - Stack Overflow ... 1 Answer. When using an entity constructor its arguments must be optional....
Read more >
EntityHelper and Decorated Entities - MikroORM
To update existing entity, we need to provide its PK in the data , as well as to load that entity first into...
Read more >
EntityManager API - typeorm - GitBook
If the entity already exists in the database, then it's updated. If the entity does not exist in the database yet, it's inserted....
Read more >
TypeORM - Quick Guide - Tutorialspoint
id is an integer, which doesn't accept duplicate values, but we need to assign values. ... @UpdateDateColumn − It is used to set...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found