GORM Playground Link
https://github.com/go-gorm/playground/pull/517
Description
Explain your user case and expected results
Use case: I want to bulk insert data with a ID primary key and two fields that represent a composite unique index. When I'm bulk creating, none of the records have IDs. If a record matches an existing composite index I want to update the fields, if it doesn't I want to insert a new row.
When defining the model I use index:composite:something_idx on the two fields. When using clause.OnConflict I specify that constraint:
result := DB.Clauses(clause.OnConflict{
if err := DB.First(&result, user.ID).Error; err != nil {
OnConstraint: "something_idx",
UpdateAll: true,
})
The SQL generated has an error because it's adding the id column as the conflict target:
INSERT INTO "things" ("created_at","updated_at","deleted_at","some_id","other_id","data") VALUES ('2022-09-21 13:08:21.078','2022-09-21 13:08:21.078',NULL,'1234','1234','something else')
ON CONFLICT ("id") ON CONSTRAINT something_idx
DO UPDATE SET "updated_at"='2022-09-21 13:08:21.078',"deleted_at"="excluded"."deleted_at","some_id"="excluded"."some_id","other_id"="excluded"."other_id","data"="excluded"."data"
RETURNING "id"
I get an error from Postgres:
ERROR: syntax error at or near "ON" (SQLSTATE 42601)
I expect the ON CONFLICT clause statement to be generated as:
ON CONFLICT ON CONSTRAINT something_idx
Am I specifying the struct tags wrong? It feels like it shouldn't be generating those column names in the ON CONFLICT clause if ON CONSTRAINT is specified.