Skip to content

Commit 6210ed1

Browse files
authored
Merge pull request #1319 from atzedus/upsert-more-convenion-naming
UpsertConflictObject -> UpsertConflictTarget+docs
2 parents 849adb0 + cf047cd commit 6210ed1

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1792,6 +1792,18 @@ p1.Name = "Hogan"
17921792
// INSERT INTO pilots ("id", "name") VALUES ($1, $2)
17931793
// ON CONFLICT ("id") DO UPDATE SET "name" = EXCLUDED."name"
17941794
err := p1.Upsert(ctx, db, true, []string{"id"}, boil.Whitelist("name"), boil.Whitelist("id", "name"))
1795+
1796+
// Custom conflict_target expression:
1797+
// INSERT INTO pilots ("id", "name") VALUES (9, 'Antwerp Design')
1798+
// ON CONFLICT ON CONSTRAINT pilots_pkey DO NOTHING;
1799+
conflictTarget := models.UpsertConflictTarget
1800+
err := p1.Upsert(ctx, db, false, nil, boil.Whitelist("id", "name"), boil.None(), conflictTarget("ON CONSTRAINT pilots_pkey"))
1801+
1802+
// Custom UPDATE SET expression:
1803+
// INSERT INTO pilots ("id", "name") VALUES (9, 'Antwerp Design')
1804+
// ON CONFLICT ("id") DO UPDATE SET (id, name) = (sub-SELECT)
1805+
updateSet := models.UpsertUpdateSet
1806+
err := p1.Upsert(ctx, db, true, []string{"id"}, boil.Whitelist("id", "name"), boil.None(), updateSet("(id, name) = (sub-SELECT)"))
17951807
```
17961808

17971809
* **Postgres**

drivers/sqlboiler-psql/driver/override/main/singleton/psql_upsert.go.tpl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
type UpsertOptions struct {
2-
conflictObject string
2+
conflictTarget string
33
updateSet string
44
}
55

66
type UpsertOptionFunc func(o *UpsertOptions)
77

8-
func UpsertConflictObject(conflictObject string) UpsertOptionFunc {
8+
func UpsertConflictTarget(conflictTarget string) UpsertOptionFunc {
99
return func(o *UpsertOptions) {
10-
o.conflictObject = conflictObject
10+
o.conflictTarget = conflictTarget
1111
}
1212
}
1313

@@ -45,8 +45,8 @@ func buildUpsertQueryPostgres(dia drivers.Dialect, tableName string, updateOnCon
4545
columns,
4646
)
4747

48-
if upsertOpts.conflictObject != "" {
49-
buf.WriteString(upsertOpts.conflictObject)
48+
if upsertOpts.conflictTarget != "" {
49+
buf.WriteString(upsertOpts.conflictTarget)
5050
} else if len(conflict) != 0 {
5151
buf.WriteByte('(')
5252
buf.WriteString(strings.Join(conflict, ", "))

0 commit comments

Comments
 (0)