DEV Community

ADEKOLA Abdwahab
ADEKOLA Abdwahab

Posted on


Foreign key constraint failed on the field (prisma )

This error comes up due to the fact that you have a foreign key requirement on your DB.

So you need to check what you inserting into the DB and ensure that your insertion(s) meets up with the foreign key requirement.

Latest comments (3)

dr1ku profile image
Cosmin Pițu

This can also happen when using upsert, in our case we had a prisma @relation tied to a foreign key/FK whose OnUpdate ReferentialAction was not changed from the default NoAction.

The rather generic error message Foreign key constraint failed on the field:fk_ <..> (index)` doesn't help much on pinpointing the cause or any direction whatsoever.

Changing the affected @relation's OnUpdate ReferentialAction to Cascade enables the update part of an upsert to go through/work as expected.

codarbind profile image
ADEKOLA Abdwahab

thank you for contributing

freedisch profile image

I got the same issue, and spent hours fixing it thanks

An Animated Guide to Node.js Event Loop

>> Check out this classic DEV post <<