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

MERGE queries shouldn't require primary key setting. #100

Open
sblackstone opened this issue May 31, 2023 · 5 comments
Open

MERGE queries shouldn't require primary key setting. #100

sblackstone opened this issue May 31, 2023 · 5 comments
Assignees

Comments

@sblackstone
Copy link

Describe the feature

Merge (upsert) queries currently require all of the columns in clause.Conflict.Columns to be primary keys, otherwise it silently falls back to a regular insert.

A table can have a primary key thats simply an auto-increment referencing the row, but also unique constraints which can trigger the ON CONFLICT case of a merge query.

Motivation

Upsert queries that aren't necessarily looking at the primary key.


	MERGE table1 AS [Target] USING (
	SELECT
	uniq_field1 = ?,
	uniq_field2   = ?,
	value = ?)
	AS [Source] ON [Target].uniq_field1 = [Source].uniq_field1 and [Target].uniq_field2 = [Source].uniq_field2
	WHEN MATCHED THEN
	UPDATE
		SET
		[Target].value=[Source].value,
		WHEN NOT MATCHED THEN
		INSERT (
			uniq_field1,
			uniq_field2,
			value)
	VALUES
		(
			[Source].uniq_field1,
			[Source].uniq_field2,
			[Source].value
		)

Related Issues

None.

@sblackstone
Copy link
Author

Additionally, the fall-back behavior when an on-conflict clause exists but doesn't match the primary keys was super-confusing and I'd suggest that it should return an error rather than just doing an insert.

@jhajjaarap
Copy link

Any good news on this issue?

@sblackstone
Copy link
Author

Would be nice to have @jhajjaarap's patch upstreamed! 🎉

ddadaal added a commit to ddadaal/gorm-sqlserver that referenced this issue Jan 16, 2024
@muety
Copy link

muety commented Jan 16, 2024

@jhajjaarap Perhaps create a pull request for upstreaming your fix? Would love to get that merged.

@huseyinkabil
Copy link

huseyinkabil commented Oct 23, 2024

I also have trouble with this topic :(

Model is:

type Model struct {
	ID                int64     `gorm:"primaryKey;column:ID"`
        AnyUniqueColumn string `gorm:"uniqueIndex;column:AnyUniqueColumn"`
}

Query is:

result := db.Model(&Model{}).
		Clauses(clause.OnConflict{
			Columns: []clause.Column{{Name: "AnyUniqueColumn"}},
			DoNothing: true,
		}).
		CreateInBatches(listOfObjects, 100)

In this case, I expect driver builds "MERGE INTO" sql. But driver builds "INSERT INTO" sql. As a result I got "duplicate key" error.

@jinzhu What do you think about this issue? Will there be any updates? 🙏

@ddadaal Can you create a pull request for your fix? 🙏

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

5 participants