Prisma Error Repair

Prisma P2002: Unique Constraint Failed on the Fields

Diagnose Prisma P2002 unique constraint failures and decide whether to reject the duplicate, update the existing record, or intentionally use upsert.

Paste Your Exact Error View Repair

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

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

  1. Capture the complete error and stack trace.
  2. Identify the first application-owned location or resource involved.
  3. Confirm the runtime value, state, response, or configuration reported by the error.
  4. Apply the smallest repair that addresses the actual cause.
  5. Retest both the successful path and the original failing path.
  6. 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 Error

Reference: Official platform documentation