Skip to content
Back to the playbook

Handling Prisma P2002 unique constraint violations

PrismaPostgreSQLrevision 1

Diagnostic tree

  1. In progressPrismaClientKnownRequestError P2002Prisma's create() (or update()) was rejected because the row it tried to write conflicts with a unique index or constraint. This is Postgres's own uniqueness check firing — Prisma is just the messenger.
View the whole map (6 more steps)
  • Is this an expected duplicate, or a bug in how the record is being created?
  • Prisma is correctly enforcing the constraint — handle it instead of preventing it
  • An unexpected duplicate means something upstream is wrong
  • Catch P2002 explicitly, or switch to upsert
  • Confirm duplicates are now handled without an unhandled exception
  • Resolved

What to test

PrismaClientKnownRequestError P2002

Prisma's create() (or update()) was rejected because the row it tried to write conflicts with a unique index or constraint. This is Postgres's own uniqueness check firing — Prisma is just the messenger.

What happened?