Repository.save() cause duplicate keys with PrimaryGeneratedColumn
See original GitHub issueIssue 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:
- Created 3 years ago
- Comments:9 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
if you have the
id
asbigint
db type, you need to set:in your db config.
REF : https://github.com/typeorm/typeorm/blob/master/docs/connection-options.md#mysql--mariadb-connection-options
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-optionsEssentially, since you’re using
bigint
for the id type, TypeORM will actually load the value as a string. Without looking at the code inMysqlQueryRunner
, 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
toint
or use a transformer in the column definition:This will force it to load as a number, although at a risk of precision loss once the values exceed
Number.MAX_SAFE_INTEGER
.