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
Sorry in advance if this is duplicated, but I did not find any open issue regarding that.
If we save multiples items using queryBuilder.insert() method, all the items will have the id overridden by latest id item inserted. Let's suppose you are inserting three Cats in the database, they will have the id 1, 2 and 3 respectively.
After queryBuilder.insert(), all of three entities id will be overridden with the latest insertedId, so all entity items will have the id 3.
I am not sure if this behavior is normal, but I got my self to struggle to understand why my entities id were all the same and after debugging, I found out that is because of queryBuilder insert method.
Lets supose that we have a CatEntity. We want to insert three cats, so we have:
After we execute this statement, a single and latest insertedId will be populated for both Cats, so typeorm will return the following entities from insert statement:
Note that even if we insert multiples items, the query runner returns only a single latest idinsertResult.raw = await queryRunner.query(sql, parameters);, which will be used later on
ReturningResultsEntityUpdator.insert to replace all entities ids.
In the database Cat1 was inserted with id 1 and Cat2 was inserted with id 2, ..... But query runner seems to return all entities with the latest inserted id., then ReturningResultsEntityUpdator.insert method will change all entities id to the latest one.
The line 113 of ReturningResultsEntityUpdator is responsible to set entity id to the latest inserted id:
this.queryRunner.manager.merge(metadata.target as any, entity, generatedMap); // todo: this should not be here, but problem with below line.
Summary:
If you insert an array of items using queryBuilder.Insert, after that, all of them will have the same id, which will be the latest insertedId.
I would be more than happy to provide more information/create a repository to reproduce or open a pull request if this behavior is not normal.
Thanks in advance!
The text was updated successfully, but these errors were encountered:
lucashfreitas
changed the title
Wrong insertedId when multiple inserts
Entity ids are all overridden by the latest inserted id when using queryBuilder.insert to save multiples items
Sep 3, 2019
Issue type:
[x] bug report
Database system/driver:
[x]
mysql
/mariadb
TypeORM version:
[x]
latest
Sorry in advance if this is duplicated, but I did not find any open issue regarding that.
If we save multiples items using
queryBuilder.insert()
method, all the items will have the id overridden by latest id item inserted. Let's suppose you are inserting three Cats in the database, they will have the id 1, 2 and 3 respectively.After queryBuilder.insert(), all of three entities id will be overridden with the latest insertedId, so all entity items will have the id 3.
I am not sure if this behavior is normal, but I got my self to struggle to understand why my entities id were all the same and after debugging, I found out that is because of queryBuilder insert method.
Lets supose that we have a CatEntity. We want to insert three cats, so we have:
catsItems: Cats[] = [ { name: 'Cat1' }, { name:'Cat2' }, { name: 'Cat3' } ]
const catRepository = connection.getRepository(Cat);
Now, lets save the cats:
After we execute this statement, a single and latest insertedId will be populated for both Cats, so typeorm will return the following entities from insert statement:
[{id:3,name:'Cat1',id:3,name:'Cat2'}. {id:3,name:'Cat3']
Note that even if we insert multiples items, the query runner returns only a single latest id
insertResult.raw = await queryRunner.query(sql, parameters);
, which will be used later onReturningResultsEntityUpdator.insert to replace all entities ids.
In the database Cat1 was inserted with id 1 and Cat2 was inserted with id 2, ..... But query runner seems to return all entities with the latest inserted id., then ReturningResultsEntityUpdator.insert method will change all entities id to the latest one.
The line 113 of ReturningResultsEntityUpdator is responsible to set entity id to the latest inserted id:
this.queryRunner.manager.merge(metadata.target as any, entity, generatedMap); // todo: this should not be here, but problem with below line
.Summary:
If you insert an array of items using queryBuilder.Insert, after that, all of them will have the same id, which will be the latest insertedId.
I would be more than happy to provide more information/create a repository to reproduce or open a pull request if this behavior is not normal.
Thanks in advance!
The text was updated successfully, but these errors were encountered: