The error
PrismaClientKnownRequestError: P2002 Unique constraint failed on the fields
Quick answer: The database rejected a Prisma create or update because the operation conflicts with a value protected by a unique constraint.
What this error means
The database rejected a Prisma create or update because the operation conflicts with a value protected by a unique constraint.
Start with the first application-owned file, operation, request, query, plugin, or component identified by the error. Fix the underlying failure before changing unrelated parts of the application.
Common causes
- A create operation attempts to reuse an existing unique value.
- An update changes a unique field to a value already owned by another record.
- A retried request attempts to create the same logical record twice.
- The application assumes a value is available without handling concurrent writes.
- Insert-or-update behavior is intended but the application uses create instead of an appropriate upsert flow.
Immediate repair pattern
const existingUser =
await prisma.user.findUnique({
where: {
email
}
});
if (existingUser) {
return {
success: false,
error: 'Email already exists'
};
}
Apply the repair only when it matches the actual runtime condition shown by the complete error and application context.
Do not hide the root cause
Do not remove a legitimate unique constraint merely to suppress P2002. Decide what the application should do when the same unique value already exists.
Repair checklist
- Capture the complete error and stack trace.
- Identify the first application-owned location or resource involved.
- Confirm the runtime value, state, response, or configuration reported by the error.
- Apply the smallest repair that addresses the actual cause.
- Retest both the successful path and the original failing path.
- Confirm the original error no longer occurs.
Still getting the error?
Paste the complete error and stack trace into 0-69. The matching repair engine can classify the failure and prepare specific repair steps and copy-ready code for the detected pattern.
Analyze My Prisma ErrorReference: Official platform documentation