GORM Playground Link

https://github.com/go-gorm/playground/pull/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"

Comment From: jinzhu

the playground seems works for me.

Comment From: paulinevos

@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.

Comment From: paulinevos

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.