go-gorm / sqlserver

GORM sqlserver driver
MIT License
56 stars 39 forks source link

clause.OnConflict is silently ignored #47

Closed selaux closed 2 years ago

selaux commented 2 years ago

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

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:

jinzhu commented 2 years ago

We should support this with merge into now.

stevefoxuser commented 1 month ago

问题仍然存在。。一样的代码,一样的报错 包的版本 gorm.io/driver/sqlserver v1.5.3 // indirect gorm.io/gorm v1.25.12 // indirect