Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entity ids are all overridden by the latest inserted id when using queryBuilder.insert to save multiples items #4682

Closed
lucashfreitas opened this issue Sep 3, 2019 · 2 comments

Comments

@lucashfreitas
Copy link

lucashfreitas commented 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:

`await catRepository
        .createQueryBuilder('cat')
        .insert()
        .values(catsItems)
        .execute();`

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 on
ReturningResultsEntityUpdator.insert to replace all entities ids.

image

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!

@lucashfreitas 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
@mollyalger
Copy link

I believe this is the same as #2131

@Kononnable
Copy link
Contributor

Kononnable commented Oct 16, 2019

Duplicate of #2131

@Kononnable Kononnable marked this as a duplicate of #4682 Oct 16, 2019
@Kononnable Kononnable marked this as a duplicate of #2131 Oct 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants