published fresh until

Prisma: Unique constraint failed—what should I change?

Summary As of October 2026, check P2002 and the unique field before changing writes; use upsert() only when update-or-create is the intended behavior. Prisma ORM v6 guidance checked 2026-10-28; concurrent upserts can still collide.

Not for connection-pool timeouts, see prisma-pool-timeout-shadow-replay-max-client-conn-errors

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

P2002 template: Unique constraint failed on the {constraint}

Prisma rejects a write involving an email that must be unique.

First action: check e instanceof Prisma.PrismaClientKnownRequestError and e.code === "P2002", then inspect the named constraint before choosing rejection or upsert().

Evidence: official documentation explains the duplicate-email example and update-or-create semantics; public reports establish that concurrent upserts can also fail, but this workflow has not been executed here.

Not for: connection-pool timeouts; see prisma-pool-timeout-shadow-replay-max-client-conn-errors.

What is established

  • Prisma's error-handling example creates an existing email and explains that @unique makes it fail. Scope is Prisma ORM v6 (checked 2026-10-28).
  • The engine source defines P2002 with a constraint placeholder. The opening is a concrete field-rendering example; other connectors can name a constraint instead (checked 2026-10-28).

How to handle the write

1. Identify the failing call and the unique field or compound constraint. Match the documented error class and code rather than parsing only the English message. Preserve unexpected errors. 2. Decide the business behavior. If an email must belong to only one account, return a duplicate outcome; updating that account is not automatically correct. 3. If the operation genuinely means update-or-create, use the official CRUD pattern. For its User schema, this updates an existing user's name or creates that user:

await prisma.user.upsert({
  where: { email: "viola@prisma.io" },
  update: { name: "Viola the Magnificent" },
  create: { email: "viola@prisma.io", name: "Viola the Magnificent" },
});

4. Keep the where selector unique. The CRUD page says non-unique fields cannot provide this find-or-create behavior. 5. If parallel calls still produce P2002, inspect the maintainer response in issue #22778 and the linked upsert guidance before choosing retry behavior. An upsert is not a blanket concurrency guarantee.

Diagnostic: Unique constraint failed

Observed situationFirst decision
Creating an existing emailReject the duplicate or intentionally update the existing record
Repeated delivery of the same eventDefine idempotency behavior before changing the write
Parallel upserts failReproduce concurrent requests; do not assume the call is atomic in every query shape
Foreign-key error insteadStop this workflow; P2003 is a different error

The engine source also defines a foreign-key constraint error, P2003. No foreign-key cause or fix is established by the guidance used here.

What to test

  • Existing email, new email, and two concurrent calls for the same new email.
  • The duplicate response does not disclose or overwrite another account's data.
  • Unrelated errors are rethrown rather than converted into duplicate outcomes.

Public evidence

  • j-christoffersen, issue #22778, 2024-01-23 (checked 2026-10-28): “Calling upsert while another record is created results in P2002”. Maintainer janpio links the documented unique-key limitation. This supports the concurrency pitfall, not a guarantee that a particular retry fixes every query.
  • maticzav, discussion #15506, 2022-09-24 (checked 2026-10-28): “If I create two entries with equal values simultaneously”. The report describes a unique error during parallel writes; it is historical evidence, not a current connector test.

Pitfalls

Removing @unique changes the data rule instead of deciding duplicate behavior. Blindly replacing account creation with upsert can overwrite an existing account. A foreign-key failure needs its own diagnosis.

A prompt for your agent

Read the linked official Prisma error-handling and CRUD pages before editing; your training data may predate this guidance. Identify the installed Prisma version, failing call, code and constraint. Show whether duplicate rejection or update-or-create matches our business rule. Preserve uniqueness, avoid logging personal data, and propose tests for existing, new and concurrent writes. Do not assume upsert eliminates races.

Limits

No application or database tests were run. Guidance is scoped to Prisma ORM v6 (checked 2026-10-28); it is not a migration guide for other versions. Constraint rendering varies. The two public reports establish observed races, not a universal cause or verified fix for every P2002. Foreign-key remediation is outside this thread.

Started from a post by @j-christoffersen, packaged by the editor. Original post

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Prisma: Unique constraint failed—what should I change?
Summary: As of October 2026, check P2002 and the unique field before changing writes; use upsert() only when update-or-create is the intended behavior. Prisma ORM v6 guidance checked 2026-10-28; concurrent upserts can still collide.
Copy for agent
get the whatworks thread prisma-p2002-unique-constraint-failed-create-or-upsert and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread prisma-p2002-unique-constraint-failed-create-or-upsert
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp