Error: Database error: Foreign key constraint failed
See original GitHub issueBug description
I have the following schema:
model User {
id String @id @default(uuid())
...
address Address? @relation(fields: [id], references:[entityId])
}
model Address {
entityId String @id
city String?
line1 String?
line2 String?
postalCode String?
user User?
}
I execute: npx prisma db push
I am using: Postgres
The error I get:
Error: Database error
Foreign key constraint failed: User_id_fkey
0: sql_migration_connector::sql_database_step_applier::apply_migration
at migration-engine/connectors/sql-migration-connector/src/sql_database_step_applier.rs:15
1: migration_core::api::SchemaPush
at migration-engine/core/src/api.rs:163
Am I missing something conceptually? The data model seems valid to me. I use the VS Code plugin to lint my schema and it’s not showing any errors.
How to reproduce
As seen above.
Expected behavior
The new data model to be pushed correctly to the database.
Prisma information
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
Version: "prisma": "^2.22.1",
Environment & setup
- OS: MacOS
- Database: PostgreSQL
- Node.js version: 12.14.0
- Prisma version: 2.22.1
prisma : 2.22.1
@prisma/client : 2.21.2
Current platform : darwin
Query Engine : query-engine 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine : migration-engine-cli 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 60cc71d884972ab4e897f0277c4b84383dddaf6c
Studio : 0.379.0
Issue Analytics
- State:
- Created 2 years ago
- Reactions:14
- Comments:17 (8 by maintainers)
Top Results From Across the Web
sqlite3 "foreign key constraint failed"
The "problem" is that you have set a foreign key on table B. foreign key(id2) references A(id). This means that column id2 in...
Read more >FOREIGN KEY Constraint Failed: SQlite3
You try to insert Albums.ArtistId value ( 7 ) which is absent in Artisis.ArtistId values list now. So insertion fails.
Read more >How to fix MySQL ERROR 1452 a foreign key constraint fails
The second way you can fix the ERROR 1452 issue is to disable the FOREIGN_KEY_CHECKS variable in your MySQL server. ... This variable...
Read more >Foreign key constraint failed on the field: foreign key #10867
I'm trying to delete a "project" row but get a warning for Foreign key constraints. This only happened after I added the member...
Read more >Can't see why: FOREIGN KEY constraint failed
As a result when the statement finishes the database is still in a non-compliant state and so the changes are rolled back and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It happened with me. I was getting the same error when creating objects inside objects in a relation from one to many.
The issue has been causd by the hierarchaly minor object beeing created before the higher one.
The solution was to seed manually object by object.
Other cause to the error was the object being created with the id bond and the “father” had a different id.
I’ve used prisma.objectname.create with id inclused. One by one.
Hope it helped.
Obs.: I thought about make an logical sequence that allows the subsquent seed to happen only after previous. Didn’t implement anything like that. If somebody implemented this solution or have a better one, please let me know.
Obrigado. =) lucas_araujo_11@hotmail.com
So in your case @archcra it was caused by a mismatch between your SQL schema pointing the Foreign Key to one table, and the Prisma Schema relation pointing to another model?