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.

Top comments (4)

Collapse
 
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.

Collapse
 
codarbind profile image
ADEKOLA Abdwahab

thank you for contributing

Collapse
 
hotarumoon profile image
Ege

thank you very much I was searching for this!

Collapse
 
freedisch profile image
Freedisch

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