Skip to content

Commit

Permalink
psql: Fix UPSERT query to respect conflict_target
Browse files Browse the repository at this point in the history
The Postgres driver, for the following method call:

    record.Upsert(ctx, db, false, []string{"foo"}, boil.None(), boil.Infer())

...would omit the conflict_target parameter[1], i.e. `foo`, from the
query:

    INSERT INTO "cars" ("id", "foo") VALUES ($1,$2) ON CONFLICT DO NOTHING

This patch changes the behavior so that the conflict_target is
respected, resulting in the following:

    INSERT INTO "cars" ("id", "foo") VALUES ($1,$2) ON CONFLICT ("foo") DO NOTHING

Fixes #1289

[1] https://www.postgresql.org/docs/15/sql-insert.html#SQL-ON-CONFLICT
  • Loading branch information
agis committed Jul 14, 2023
1 parent b77089f commit a0ded2f
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ func buildUpsertQueryPostgres(dia drivers.Dialect, tableName string, updateOnCon
columns,
)

buf.WriteByte('(')
buf.WriteString(strings.Join(conflict, ", "))

if !updateOnConflict || len(update) == 0 {
buf.WriteString("DO NOTHING")
buf.WriteString(") DO NOTHING")
} else {
buf.WriteByte('(')
buf.WriteString(strings.Join(conflict, ", "))
buf.WriteString(") DO UPDATE SET ")
for i, v := range update {
Expand Down

0 comments on commit a0ded2f

Please # to comment.