Prisma P2002: Unique constraint failed on the {constraint}
Prisma says P2002 Unique constraint failed on the {constraint}? That write hit a unique constraint. The message names the constraint.
Reviewed October 22, 2026
What it means in plain English
Prisma's error reference defines P2002 as: Unique constraint failed on the {constraint}.
Prisma Client throws PrismaClientKnownRequestError when the query engine returns a known error related to the request. The reference gives a unique constraint violation as the example. That error has code, meta, message, and clientVersion. meta is additional information about the error. The reference's example of the field that caused the error is { target: [ 'email' ] }.
P2003 is a different code: Foreign key constraint failed on the field {field_name}. P2025 means an operation failed because it depends on one or more records that were required but not found. P2021 means the table {table} does not exist in the current database.
Likely causes
- The write uses a value that fails the unique constraint named in the P2002 message.
meta.targetnames the field. The reference's example isemail.- The failure is a foreign key, a missing required record, or a missing table. Those are
P2003,P2025, andP2021on the same page.
How to fix it, step by step
- Read
code. Stay on this page when it isP2002. - Read
{constraint}in the message, and readmetafor the extra detail. The reference's example of that detail is{ target: [ 'email' ] }. - P2002 is the failure of the unique constraint named in the message. The reference does not add another procedure.
metais where it puts extra detail, and its example of the field that caused a known request error is{ target: [ 'email' ] }. - If the code is
P2003, the failure is the foreign key on{field_name}, not a unique constraint. - If the code is
P2021, the table in the message is not in the current database. If the code isP2025, a required record was not found.
How to check it worked
- The same write no longer throws
P2002. codeon the next error, if there is one, is notP2002.
Sources
Related fixes
More Database connections fixes
Keep going on your own.
The guides stay free. A deep leak report is $15. Fix packs are $24, or $49 for all three. There is no done-for-you repair.