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

Auto incrementing column does not auto-increment with Bulk insertion #6025

Closed
TranBaVinhSon opened this issue May 7, 2020 · 3 comments
Closed

Comments

@TranBaVinhSon
Copy link

TranBaVinhSon commented May 7, 2020

Issue type:

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

Database system/driver:

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

TypeORM version:

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

Steps to reproduce or a small repository showing the problem:
I'm using bulk insertion to insert many records to DB as its the most efficient way in terms of performance.
https://github.com/typeorm/typeorm/blob/master/docs/insert-query-builder.md#insert-using-query-builder

But the auto-incrementing column doesn't work as I expected

Here is my code
Migration

columns: [
  {
    name: "id",
    type: "int",
    isPrimary: true,
    isNullable: false,
    isGenerated: true,
    generationStrategy: "increment", // auto increment
  },
];

My Model

@Entity()
export class Page {
  @PrimaryGeneratedColumn({
    unsigned: true,
  })
  id: number;

  @Column("name")
  name: string;

  @ManyToOne(type => Report, report => report.pages)
  report: Report;

  @OneToMany(type => Shape, shape => shape.page)
  shapes: Shape[];
}

Query function

  async create(queryRunner: QueryRunner, pages: Page[]): Promise<void> {
    const insertedResults = await queryRunner.connection
      .createQueryBuilder()
      .insert()
      .into(Page)
      .values(pages)
      .execute();
  }

when I inserted 4 pages to the database, the result looks like this

console.log("before call create()", pages)
await create(queryRunner, pages);
console.log("after call create()", pages);
// before call create()
  [
    {
      "name": "name 1"
    },
    {
      "name": "name 2"
    }
    {
      "name": "name 3"
    }
    {
      "name": "name 4"
    }
  ]
// after call create()
  [
    {
      "id": 1,
      "name": "name 1"
    },
    {
      "id": 1,  // should be 2
      "name": "name 2"
    }
    {
      "id": 1, // should be 3
      "name": "name 3"
    }
    {
      "id": 1, // should be 4
      "name": "name 4"
    }
  ]

It seems the only problem with bulk insertion. When I try to insert one record per query, I got the ID as I expected. But inserting one record per query is a really bad way if you want to insert many records.

Any advice is welcome!

@elad-yosifon
Copy link

👍
happens with postgresql as well

@Talv
Copy link

Talv commented Oct 25, 2020

It was fixed by #6668 which was released in 0.2.26. Make sure you're on latest version.

@imnotjames
Copy link
Contributor

Well, close enough to fixed. We can't actually get the values back again during this operation. ): So we guess at what they should be. It's a temporary fix until something more exact is available.

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

No branches or pull requests

4 participants