Skip to content

Query the same ID after inserting #2870

Closed

Description

@lxzggaa

Issue type:

[x] question
[ ] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[x] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

    const arr = [
        {name: 'name1', identify: 'identify1'},
        {name: 'name2', identify: 'identify2'},
        {name: 'name3', identify: 'identify3'},
      ]
      
      await resourceRepository.insert(arr)

SQL:

query: INSERT INTO `resource`(`id`, `name`, `identify`) VALUES (DEFAULT, ?, ?), (DEFAULT, ?, ?), (DEFAULT, ?, ?) -- PARAMETERS: ["name1","identify1","name2","identify2","name3","identify3"]
query: SELECT `Resource`.`id` AS `Resource_id`, `Resource`.`name` AS `Resource_name` FROM `resource` `Resource` WHERE (`Resource`.`id` = ?) -- PARAMETERS: [1]
query: SELECT `Resource`.`id` AS `Resource_id`, `Resource`.`name` AS `Resource_name` FROM `resource` `Resource` WHERE (`Resource`.`id` = ?) -- PARAMETERS: [1]
query: SELECT `Resource`.`id` AS `Resource_id`, `Resource`.`name` AS `Resource_name` FROM `resource` `Resource` WHERE (`Resource`.`id` = ?) -- PARAMETERS: [1]

The same ID was queried three times

Activity

  1. mario-bware commented on Dec 18, 2018

    @mario-bware

    I've just run into this same issue. Makes the insert query builder unusable as you end up with incorrect duplicate PK's in your entities.

    Also, is there a way to disable the select after insert? It would be nice if the selects were optimised rather than one per record.

  2. pleerock commented on Dec 19, 2018

    @pleerock
    Member

    Actually, it wasn't supposed for insert to execute any additional query or return InsertResult result. So looks like a bug.

    For now, to fix your issues you need to create insert query builder instead of using insert method from manager. Example:

            manager.createQueryBuilder()
                .insert()
                .into(EntityCls)
                .values(entities)
                .updateEntity(false)
                .execute();
  3. mario-bware commented on Jan 7, 2019

    @mario-bware

    In my case, I was using the createQueryBuilder() and had the same issue. Including updateEntity(false) removes the bad selects after the insert and allows me to optimise the query so that is helpful, thank you!

  4. self-assigned this
    on Oct 3, 2020
  5. imnotjames commented on Oct 10, 2020

    @imnotjames
    Contributor

    Is this still a problem? We made a few changes that might have fixed this problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions