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:
Émile Ré
2026-05-19 11:03:37 +04:00
parent f81168bc18
commit f1357a7e20
2 changed files with 32 additions and 1 deletions

View File

@@ -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
}
}