question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Repository.save() cause duplicate keys with PrimaryGeneratedColumn

See original GitHub issue

Issue type:

[x] question [ ] 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:

[ ] latest [ ] @next [x] 0.2.24

Steps to reproduce or a small repository showing the problem:

entity

export class PostType {
  @PrimaryGeneratedColumn({
    type: 'bigint',
    unsigned: true,
    name: 'id',
  })
  id: number


  @Column({
    type: 'bigint',
    unsigned: true,
    name: 'wp_termid',
    nullable: true
  })
  wpTermid: number


  @Column({
    type: 'text',
    name: 'name'
  })
  name: string
}

controller

export class PostTypeController {
  private repository: Repository<PostType> = getRepository(PostType);

  async update() {
    let rawdata: Buffer = readFileSync(resolve(__dirname, '../data/post_types.json'))
    let data = JSON.parse(rawdata.toString())
    await this.repository.save(data)
  }
}

post_type.json

[
  {
    "id": 1,
    "wpTermid": 409,
    "name": "a"
  },
  {
    "id": 2,
    "wpTermid": 410,
    "name": "b"
  },
  {
    "id": 3,
    "wpTermid": 411,
    "name": "c"
  },
  {
    "id": 4,
    "wpTermid": 412,
    "name": "d"
  },
  {
    "id": 5,
    "wpTermid": 413,
    "name": "e"
  },
  {
    "id": 6,
    "wpTermid": 414,
    "name": "f"
  }
]

error info

QueryFailedError: ER_DUP_ENTRY: Duplicate entry '1' for key 'post_types.PRIMARY'
    at new QueryFailedError (/Users/almon/code/node_artical_push/src/error/QueryFailedError.ts:9:9)
    at Query.<anonymous> (/Users/almon/code/node_artical_push/src/driver/mysql/MysqlQueryRunner.ts:167:37)
    at Query.<anonymous> (/Users/almon/code/node_artical_push/node_modules/mysql/lib/Connection.js:526:10)
    at Query._callback (/Users/almon/code/node_artical_push/node_modules/mysql/lib/Connection.js:488:16)
    at Query.Sequence.end (/Users/almon/code/node_artical_push/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
    at Query.ErrorPacket (/Users/almon/code/node_artical_push/node_modules/mysql/lib/protocol/sequences/Query.js:92:8)
    at Protocol._parsePacket (/Users/almon/code/node_artical_push/node_modules/mysql/lib/protocol/Protocol.js:291:23)
    at Parser._parsePacket (/Users/almon/code/node_artical_push/node_modules/mysql/lib/protocol/Parser.js:433:10)
    at Parser.write (/Users/almon/code/node_artical_push/node_modules/mysql/lib/protocol/Parser.js:43:10)
    at Protocol.write (/Users/almon/code/node_artical_push/node_modules/mysql/lib/protocol/Protocol.js:38:16) {

I’m not sure. this is a bug or a rule that typeorm cannot use save() on auto increment key? does anyone can help?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
tarekbazinecommented, Jul 2, 2021

if you have the id as bigint db type, you need to set:

    supportBigNumbers: true,
    bigNumberStrings: false,

in your db config.

REF : https://github.com/typeorm/typeorm/blob/master/docs/connection-options.md#mysql--mariadb-connection-options

2reactions
BTOdellcommented, May 14, 2020

This error could be caused by the bigNumberStrings feature in the connection options: https://github.com/typeorm/typeorm/blob/master/docs/connection-options.md#mysql--mariadb-connection-options

Essentially, since you’re using bigint for the id type, TypeORM will actually load the value as a string. Without looking at the code in MysqlQueryRunner, I would assume that it uses strict equality to find existing primary keys and it crashes when it attempts to insert the data instead of update it.

Either change the datatype from bigint to int or use a transformer in the column definition:

@PrimaryGeneratedColumn({
    type: 'bigint',
    unsigned: true,
    name: 'id',
    transformer: { from: Number, to: Number }
  })
  id: number

This will force it to load as a number, although at a risk of precision loss once the values exceed Number.MAX_SAFE_INTEGER.

Read more comments on GitHub >

github_iconTop Results From Across the Web

orm - TypeORM - duplicate key value violation - Stack Overflow
In TypeORM the decorator @PrimaryGeneratedColumn() creates a primary column which the value will be generated using a sequence.
Read more >
How to Avoid Duplicate Records in Hibernate/Spring Data JPA
Learn all about how to avoid duplicate records in Hibernate/Spring Data JPA. The cause is typically due to left joins in your objects!...
Read more >
typeorm/typeorm - Gitter
hello guys. Anyone tried to implement softDelete in a 1:n relationship? I keep trying but no luck seems like its not working. I...
Read more >
Hidden dangers of duplicate key violations in PostgreSQL and ...
The “duplicate key violates unique constraint” error notifies the caller that a retry is needed. This seems like an intuitive approach, ...
Read more >
DuplicateKeyException (Spring Framework 6.0.3 API)
... necessarily a purely relational concept; unique primary keys are required by most database types. ... DuplicateKeyException(String msg, Throwable cause).
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