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.

Oldest comments (3)

freedisch profile image

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

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

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.