GORM Playground Link

https://github.com/go-gorm/playground/pull/718

Description

If you create an entity with primary ID assigned to it, gorm.Create inserts the record but doesn't commit the sequence value. By this, i mean, when the autosequence arrives to the ID you provided at some point, it still will try to use the ID for which a record is already there, resulting in pkey violation error. Well, the next time you try to create with autosequence, it proceeds to the next value. But, i guess relying on that shouldn't be the case. It might have implications if you are migrating some records on Startup using db.Create. At some point, those IDs will collide with the one from auto-sequence. Looking for answers or maybe possible fixes. Database used: POSTGRES

Comment From: gg1229505432

The database is designed like this, if there is already a pkid, then you can only update, and the primary key index is unique

Comment From: gg1229505432

If you want to migrate some data, then you can remove the pkid from the code and let him insert it himself. If you have to use pkid, then you need db.update()

Comment From: manish-kharel

Not sure if you understood the issue correctly.
The problem is, gorm is auto-assigning an ID that is already used in the database. This happens if you've previously created a Record, where you assigned the ID yourself. And after, when auto-sequence value arrives to the manually inserted value, it'll still try to use the same value regardless of the fact that the record is already there giving out pkey violation

Comment From: a631807682

The problem is, gorm is auto-assigning an ID that is already used in the database.

Gorm will not auto-assigning id

This happens if you've previously created a Record, where you assigned the ID yourself.

Yes, this is the basic functionality of the database, you can specify the id. But gorm will not process the sequence additionally.

In general, Gorm will provide some easy-to-use functions, which have basically the same semantics as the database. But it won't provide magic, as this would make it very different from directly operating the database, thereby increasing the user's learning cost.