Prisma · query error · P2002

P2002: Unique constraint failed

what it looks like
PrismaClientKnownRequestError:
Invalid `db.user.create()` invocation:

Unique constraint failed on the fields: (`email`)

What it means

You tried to create or update a row with a value that must be unique (an email, a Stripe customer id, a slug) and another row already has it.

Common causes

  • Sign-up or seed code that always calls create, even when the record exists
  • A Stripe webhook delivered twice, each delivery creating the same row
  • Two requests racing to create the same record

How to fix it

  1. Use upsert when "create or update" is what you mean
  2. Catch the error by code: error instanceof Prisma.PrismaClientKnownRequestError && error.code === "P2002", and show a friendly message
  3. For webhooks, store the event id with a unique constraint and skip duplicates
src/lib/customers.ts
await db.user.upsert({
  where: { email },
  update: { stripeCustomerId },
  create: { email, stripeCustomerId },
});

works with Prisma ORM 6.19 · Next.js 16last verified

Prompt for Cursor or Claude Code

Prisma throws P2002 Unique constraint failed. Find the create or update that hits it, decide whether it should be an upsert, and otherwise catch Prisma.PrismaClientKnownRequestError with code P2002 and return a clear error to the user. Don't remove the @unique from the schema.

Not quite your error? Paste yours into the free VibeBoot Doctor or browse the Prisma error fixer.

next: skip the next one

Skip the setup. The VibeBoot SaaS Starter ships a ready prisma/schema.prisma and one Prisma client in src/lib/db.ts (SQLite by default, Postgres by changing the provider), plus auth and Stripe on Next.js 16.4.3. $49, one-time.

14-day money-back guarantee · lifetime updates

$ fixed it? get the next one first

New fixes for what AI agents break, by email.

When we add a fix or a Cursor rule for a new Next.js, Auth.js, Prisma or Stripe break, it goes out in the VibeBoot newsletter. Free, occasional, unsubscribe in one click.

Subscribe free