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

Insert record with ID assigned doesn't commit the sequence value #6968

Closed
manish-kharel opened this issue Apr 16, 2024 · 4 comments
Closed
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@manish-kharel
Copy link

GORM Playground Link

go-gorm/playground#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

@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label Apr 16, 2024
@gg1229505432
Copy link

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

@gg1229505432
Copy link

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()

@manish-kharel
Copy link
Author

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

@a631807682
Copy link
Member

a631807682 commented Apr 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

4 participants