If I use the following code with SQL Server I expect the selected row to be updated when I call this code a second time (this is a short excerpt of the code, for the full code see the example repo above).
type TestModel struct {
gorm.Model
Matcher string `gorm:"uniqueIndex;size:512"`
Data string
}
result := db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "matcher"}},
DoUpdates: clause.Assignments(map[string]interface{}{"age": 55}),
}).Create(&User{
Name: "foobar",
Age: 30,
})
Instead the following error is thown: mssql: Cannot insert duplicate key row in object 'dbo.test_models' with unique index 'idx_test_models_matcher'. The duplicate key value is (foobar).
I am aware that SQL Server does not support the ON CONFLICT clause directly, but there are existing solutions to work around this, e.g. here or here. It would be nice for GORM to either:
Map the clause.OnConflict clause to one of those implementations
Return an error that explicitly states that clause.OnConflict is not supported for SQL Server and add this information to GORMs documentation
Reproduction Link
Unfortunately I cannot provide a playground link as I am on an ARM system, but here is a link to a reproduction repo.
https://github.com/selaux/gorm-sql-server-upsert-issue
Description
If I use the following code with SQL Server I expect the selected row to be updated when I call this code a second time (this is a short excerpt of the code, for the full code see the example repo above).
Instead the following error is thown:
mssql: Cannot insert duplicate key row in object 'dbo.test_models' with unique index 'idx_test_models_matcher'. The duplicate key value is (foobar).
I am aware that SQL Server does not support the
ON CONFLICT
clause directly, but there are existing solutions to work around this, e.g. here or here. It would be nice for GORM to either:clause.OnConflict
clause to one of those implementationsclause.OnConflict
is not supported for SQL Server and add this information to GORMs documentation