go-gorm / go-gorm/sqlserver

MERGE queries shouldn't require primary key setting.

Open
#100 6 comments 1 reaction 1 assignee Claimed by @jinzhu View on GitHub
Dominant language
Go
Stars
66
Forks
52
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.