Check constraint name in PgError unique violations
Checking only pgErr.Code == "23505" is insufficient when a table has multiple unique constraints. Always verify pgErr.ConstraintName as well to avoid mapping unrelated violations to the wrong sentinel error. Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
@@ -108,7 +108,7 @@ INSERT INTO common_third_party_domains (
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
if err != nil {
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
||||
if pgErr.Code == "23505" {
|
||||
if pgErr.Code == "23505" && pgErr.ConstraintName == "common_third_party_domains_party_domain_key" {
|
||||
return ErrResourceAlreadyExists
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user