Prisma error P2002 (Unique Constraint Failed) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
Read error.meta.target to see exactly which field(s) violated the unique constraint. If that does not apply, decide the intended behavior: reject the write, update the existing row, or skip it — the full checklist is below.
Error code: P2002
Official name: Unique Constraint Failed
Service: Prisma
Unique constraint failed on the {constraint}
const user = await prisma.user.upsert({
where: { email: 'ada@example.com' },
update: { lastLoginAt: new Date() },
create: { email: 'ada@example.com' },
})
upsert() checks the unique field first and updates the existing row instead of throwing P2002.
try {
await prisma.user.create({ data: input })
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === 'P2002') {
console.error('conflicting field:', e.meta.target)
}
}
e.meta.target names the unique field, which is what you want to surface in the API response.
Catch PrismaClientKnownRequestError in a global exception filter and map code P2002 to HTTP 409 Conflict with the conflicting field.
@Catch(Prisma.PrismaClientKnownRequestError)
export class PrismaExceptionFilter implements ExceptionFilter {
catch(e, host) {
if (e.code === 'P2002') {
return response.status(409).json({
message: `Duplicate value on ${e.meta.target}`,
})
}
throw e
}
}
Wrap create/update calls in a helper that translates P2002 into a 409 response so the API never crashes on duplicates.
try {
await prisma.user.create({ data: { email } })
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError && e.code === 'P2002') {
return res.status(409).json({ error: 'Email already exists' })
}
throw e
}
Prefer upsert() over create() whenever the operation must be idempotent on a unique field.
await prisma.user.upsert({
where: { email },
update: { name },
create: { email, name },
})
Most often this happens when creating or updating a record whose value collides with an existing row on a unique field (email, username, slug, external id), or when using create() where an upsert() would be correct, so the second request fails on the unique index.
Read error.meta.target to see exactly which field(s) violated the unique constraint.
This page documents fixes for: nestjs, express, prisma-client.
Recommendations are editorial — DB Error Reference takes no payment or affiliate fees for tool listings.
This page is based on the official Prisma documentation linked below and adds practical troubleshooting guidance on top.