Skip to content

Commit 6165fc5

Browse files
committed
Create pgsql constraint without function
1 parent 9233a43 commit 6165fc5

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

database/query_builder.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,14 @@ func (qb *queryBuilder) UpsertStatement(stmt UpsertStatement) (string, int, erro
7373
clause = "ON DUPLICATE KEY UPDATE"
7474
setFormat = `"%[1]s" = VALUES("%[1]s")`
7575
case PostgreSQL:
76-
clause = fmt.Sprintf(
77-
"ON CONFLICT ON CONSTRAINT %s DO UPDATE SET",
78-
qb.getPgsqlOnConflictConstraint(stmt.Entity()),
79-
)
76+
var constraint string
77+
if constrainter, ok := stmt.Entity().(PgsqlOnConflictConstrainter); ok {
78+
constraint = constrainter.PgsqlOnConflictConstraint()
79+
} else {
80+
constraint = "pk_" + into
81+
}
82+
83+
clause = fmt.Sprintf("ON CONFLICT ON CONSTRAINT %s DO UPDATE SET", constraint)
8084
setFormat = `"%[1]s" = EXCLUDED."%[1]s"`
8185
case SQLite:
8286
clause = "ON CONFLICT DO UPDATE SET"
@@ -301,14 +305,3 @@ func (qb *queryBuilder) BuildColumns(entity Entity, columns []string, excludedCo
301305

302306
return entityColumns[:len(entityColumns):len(entityColumns)]
303307
}
304-
305-
// getPgsqlOnConflictConstraint returns the constraint name of the current [QueryBuilderOld]'s subject.
306-
// If the subject does not implement the PgsqlOnConflictConstrainter interface, it will simply return
307-
// the table name prefixed with `pk_`.
308-
func (qb *queryBuilder) getPgsqlOnConflictConstraint(entity Entity) string {
309-
if constrainter, ok := entity.(PgsqlOnConflictConstrainter); ok {
310-
return constrainter.PgsqlOnConflictConstraint()
311-
}
312-
313-
return "pk_" + TableName(entity)
314-
}

0 commit comments

Comments
 (0)