Fix PrismaClientKnownRequestError P2002: 'Unique constraint failed on the fields: (`email`)'
Handle duplicate key violations gracefully with `upsert`, `createMany({ skipDuplicates: true })`, or sequence resynchronization in PostgreSQL.
PrismaClientKnownRequestError: Invalid `prisma.user.create()` invocation: Unique constraint failed on the fields: (`email`)
-- If P2002 happens on 'id' (autoincrement out of sync in Postgres), run:
SELECT setval(pg_get_serial_sequence('"User"', 'id'), COALESCE(MAX(id), 1)) FROM "User";
-- Or switch to collision-free time-ordered UUIDv7 via Castov CLI:
-- npx castov idRoot Cause & Mechanism Analysis
Why does this error occur?
An `INSERT` or `UPDATE` attempted to write a value that already exists in a `@unique` or `@id` column, or an auto-incrementing PostgreSQL `SERIAL` sequence fell out of sync after manual data imports.
Frequently Asked Questions
Why does P2002 happen on `id` even when I don't pass `id` in `prisma.create()`?
Need Zero-Knowledge .env Secret Sync Across Your Engineering Team?
Explore Castov Env Vault for client-side AES-256-GCM encrypted environment sharing, or sponsor this engineering hub.
Related Developer Error Solutions
Browse All 54 GuidesPostgres 42P05: prepared statement "s0" already exists (Transaction Pooler)
Configure Prisma, Drizzle, and `postgres.js` for Supabase Supavisor / PgBouncer transaction mode pooling on port 6543.
Postgres 42501: new row violates row-level security policy
Resolve PostgreSQL and Supabase Row-Level Security (RLS) INSERT/UPDATE permission errors with proper auth.uid() policies.
Next.js Hydration Failed
Resolve Next.js React hydration mismatch errors caused by browser-only APIs (window, localStorage), timestamps, or invalid HTML nesting.
CORS 'Access-Control-Allow-Origin' Missing
Resolve browser cross-origin resource sharing errors by configuring backend HTTP headers and handling preflight OPTIONS requests.