DEV Community

ADEKOLA Abdwahab
ADEKOLA Abdwahab

Posted on

1

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

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay