GORM Playground Link
I will try to prepare a Playground PoC for this issue.
Description
We are using Gorm with Postgres gorm.io/driver/postgres v1.4.6. On Gorm version gorm.io/gorm v1.24.4, upserts no longer update the primary key of the object if the object was updated instead of created. Gorm still queries PG with RETURNING "id" but does not update the ID in the struct. As a result, the ID/primary key of the object is wrong and our subsequent code fails.
// Set ID to random value (for insert).
availability := models.Availability{ID: "newinsert"}
// Upsert (no error)
db.Clauses(clause.OnConflict{
DoUpdates: availability.IntoAssignmentMap(), // Does not update ID
Columns: []clause.Column{{Name: "route_id"}, {Name: "date"}},
}).Create(&availability)
// v1.24.3: `availability.ID` is set to the ID of the updated object in Postgres.
// v1.24.4: `availability.ID` is kept as "newinsert", but ID in Postgres is actually different.
Affected Versions
All using gorm.io/driver/postgres v1.4.6:
* gorm.io/gorm v1.24.3 ✅ Works correctly
* gorm.io/gorm v1.24.4 ❌ Does not update primary key of object
* gorm.io/gorm v1.24.5 ❌ Does not update primary key of object
Comment From: github-actions[bot]
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨