Prisma error P2025: An operation failed because it depends on one or more records that were required but not found
Quick answer
P2025 fires when you `update`, `delete`, or `connect` a record by ID that doesn't exist in the database. Check existence first with `findUnique`, or catch the error and return a proper 404 instead of a raw crash.
What causes this error
Unlike P2002 (duplicate), P2025 means Prisma tried to find a specific record to update, delete, or connect a relation to — and it wasn't there. This is common when a record was already deleted, or the ID came from stale client-side data.
The Fix
try {
await prisma.article.update({ where: { id }, data });
} catch (err) {
if (err instanceof Prisma.PrismaClientKnownRequestError && err.code === 'P2025') {
throw new ApiError(404, 'Article not found');
}
throw err;
}Common causes / variations
- Deleting a record twice (double-click, retried request)
- Frontend holding a stale ID after the record was deleted elsewhere
- Trying to `connect` a relation to a foreign key that doesn't exist
Related errors
See also: P2002 Unique constraint failed.
Was this fix helpful?
Comments
Loading comments...