Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clause.OnConflict is silently ignored #47

Closed
selaux opened this issue Nov 23, 2021 · 1 comment
Closed

clause.OnConflict is silently ignored #47

selaux opened this issue Nov 23, 2021 · 1 comment
Assignees

Comments

@selaux
Copy link

selaux commented Nov 23, 2021

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:

  • 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
@jinzhu
Copy link
Member

jinzhu commented Oct 8, 2022

We should support this with merge into now.

@jinzhu jinzhu closed this as completed Oct 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants