Skip to content

adapter-pg: mapDriverError drops error.constraint for 23505 but uses it for 23503 #29495

Closed

Description

@timhaines

Bug description

@prisma/adapter-pg's mapDriverError treats error.constraint (populated by node-postgres from PG_DIAG_CONSTRAINT_NAME) asymmetrically between the unique-violation and foreign-key-violation branches.

Reference: @prisma/[email protected]/dist/index.mjs, function mapDriverError:

case "23505": {
  const fields = error.detail?.match(/Key \(([^)]+)\)/)?.at(1)?.split(", ");
  return {
    kind: "UniqueConstraintViolation",
    constraint: fields !== void 0 ? { fields } : void 0
  };
}
case "23503": {
  let constraint;
  if (error.column) {
    constraint = { fields: [error.column] };
  } else if (error.constraint) {
    constraint = { index: error.constraint };
  }
  return { kind: "ForeignKeyConstraintViolation", constraint };
}

The 23505 branch ignores error.constraint entirely. @prisma/adapter-mysql preserves it as constraint.index on 1062 (see #29344), so Postgres is the outlier here.

Why this matters

For partial unique indexes the column list is ambiguous. Two unique constraints on the same table can share key columns — e.g. one full unique on (user_id, account_id) and one partial unique on (account_id) WHERE role = 'owner'. Both surface as 23505, and the current mapping produces identical constraint.fields for both, so callers can't tell which constraint fired without regex-matching originalMessage (which per #28281 is not public API).

Suggested fix

Preserve the constraint name when it's present, matching the shape the 23503 branch already uses:

case "23505": {
  const fields = error.detail?.match(/Key \(([^)]+)\)/)?.at(1)?.split(", ");
  let constraint;
  if (error.constraint) {
    constraint = { index: error.constraint };
  } else if (fields) {
    constraint = { fields };
  }
  return { kind: "UniqueConstraintViolation", constraint };
}

The UniqueConstraintViolation MappedError type in @prisma/driver-adapter-utils already accepts { index: string } as a valid shape, so no public-API change is required.

Versions

  • @prisma/adapter-pg 7.7.0
  • @prisma/client 7.7.0
  • @prisma/driver-adapter-utils 7.7.0
  • Postgres 18

Activity

  1. added
    bug/1-unconfirmed
    kind/bug
    on Apr 21, 2026
  2. BootstrapperSBL commented on Apr 23, 2026

    @BootstrapperSBL

    Took a look — the fix is to mirror the 23503 branch's index: error.constraint fallback in the 23505 unique-violation branch. Partial unique indexes make the column-list alone ambiguous, and mysql already preserves it via #29344, so postgres is the odd one out. I'll send a PR.

  3. added a commit that references this issue on May 24, 2026
    ec08106
  4. added a commit that references this issue on Jul 22, 2026
    800f1d1
  5. added a commit that references this issue on Aug 9, 2026
    c0dfc64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/1-unconfirmedkind/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions