diff --git a/drivers/sqlboiler-psql/driver/override/main/17_upsert.go.tpl b/drivers/sqlboiler-psql/driver/override/main/17_upsert.go.tpl index 41c2485f5..7e6dca33a 100644 --- a/drivers/sqlboiler-psql/driver/override/main/17_upsert.go.tpl +++ b/drivers/sqlboiler-psql/driver/override/main/17_upsert.go.tpl @@ -103,7 +103,11 @@ func (o *{{$alias.UpSingular}}) Upsert({{if .NoContext}}exec boil.Executor{{else } conflict := conflictColumns - if len(conflict) == 0 { + if len(conflict) == 0 && updateOnConflict && len(update) != 0 { + if len({{$alias.DownSingular}}PrimaryKeyColumns) == 0 { + return errors.New("{{.PkgName}}: unable to upsert {{.Table.Name}}, could not build conflict column list") + } + conflict = make([]string, len({{$alias.DownSingular}}PrimaryKeyColumns)) copy(conflict, {{$alias.DownSingular}}PrimaryKeyColumns) } 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 1b39cd8e0..edb1b09a1 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,13 +21,16 @@ func buildUpsertQueryPostgres(dia drivers.Dialect, tableName string, updateOnCon columns, ) - buf.WriteByte('(') - buf.WriteString(strings.Join(conflict, ", ")) + if len(conflict) != 0 { + buf.WriteByte('(') + buf.WriteString(strings.Join(conflict, ", ")) + buf.WriteString(") ") + } if !updateOnConflict || len(update) == 0 { - buf.WriteString(") DO NOTHING") + buf.WriteString("DO NOTHING") } else { - buf.WriteString(") DO UPDATE SET ") + buf.WriteString("DO UPDATE SET ") for i, v := range update { if len(v) == 0 {