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

No autoincrement for serial column #5105

Closed
madskrogh opened this issue Feb 22, 2022 · 3 comments
Closed

No autoincrement for serial column #5105

madskrogh opened this issue Feb 22, 2022 · 3 comments
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@madskrogh
Copy link

madskrogh commented Feb 22, 2022

GORM Playground Link

go-gorm/playground#439

Description

Upgrading from gorm v1.22.5 columns of type serial does not appear to increment appropriately. According to the logs, with the new version of gorm and the gorm postgres driver, 0 is inserted in the serial column as opposed to the previous version where nothing is inserted and the column default sequence is correctly used.

  • gorm v1.3.0, postgress driver v1.3.0: INSERT INTO "tests" ("id","created_at","updated_at","deleted_at") VALUES (0,'2022-02-22 12:52:02.765','2022-02-22 12:52:02.765',NULL)

  • gorm v1.22.5, postgress driver v1.2.3: INSERT INTO "tests" ("created_at","updated_at","deleted_at") VALUES ('2022-02-22 12:51:00.618','2022-02-22 12:51:00.618',NULL) RETURNING "id"

@jinzhu
Copy link
Member

jinzhu commented Jun 8, 2022

the playground seems works for me.

@jinzhu jinzhu closed this as completed Jun 8, 2022
@paulinevos
Copy link

@madskrogh Did you ever figure out what happened here..? I'm having the same issue. A new entity is initialized with ID 0 and inserted as such.

@paulinevos
Copy link

Figured it out on my end, if anyone else happens to land on this issue. This is specific to Postgres, not Gorm.

I had this same issue. It happened because I had inserted other records previously and passed an ID explicitly (for seeding fixtures). If you do this, Postgres will not update the sequential IDs and will attempt to create an ID where it left off. You'll notice that if you keep trying the same insert, it will eventually work as Postgres keeps calling next_val on your erroneous queries until it hits an ID that is not taken.

See this SO question for more info. Hope that helps some other poor soul.

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

3 participants