How to Fix Prisma Error P2002: Unique constraint failed on the constraint
Prisma Client threw error code P2002 because a create or update query attempted to insert a record with a value that already exists in a column marked @unique or @@id.
PrismaClientKnownRequestError: Unique constraint failed on the fields: (email)
Quick Fix Checklist
- Use prisma.model.upsert() instead of prisma.model.create() if updating an existing record is acceptable
- Catch error instance of Prisma.PrismaClientKnownRequestError and check err.code === "P2002"
- Return a descriptive 409 Conflict HTTP response instead of an unhandled 500 server crash
- Add client-side debouncing on submit buttons to prevent double-click race conditions