From a0ded2fa305d1ebaeb417fbc83661ada08c20ef1 Mon Sep 17 00:00:00 2001 From: Agis Anastasopoulos Date: Thu, 13 Jul 2023 15:02:39 +0300 Subject: [PATCH] psql: Fix UPSERT query to respect conflict_target 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 --- .../driver/override/main/singleton/psql_upsert.go.tpl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/drivers/sqlboiler-psql/driver/override/main/singleton/psql_upsert.go.tpl b/drivers/sqlboiler-psql/driver/override/main/singleton/psql_upsert.go.tpl index 1fe63a3d7..1b39cd8e0 100644 --- a/drivers/sqlboiler-psql/driver/override/main/singleton/psql_upsert.go.tpl +++ b/drivers/sqlboiler-psql/driver/override/main/singleton/psql_upsert.go.tpl @@ -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 {