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
@uniquemakes 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 situation | First decision |
|---|---|
| Creating an existing email | Reject the duplicate or intentionally update the existing record |
| Repeated delivery of the same event | Define idempotency behavior before changing the write |
| Parallel upserts fail | Reproduce concurrent requests; do not assume the call is atomic in every query shape |
| Foreign-key error instead | Stop 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