Cascade delete does not work unless relation is optional
See original GitHub issueBug description
When I set db CONSTRAINT to CASCADE on delete and relation is marked as required in schema, prisma errors with
The change you are trying to make would violate the required relation 'UserToPost' between the `User` and `Post` models.
It works when I set the foreign key and the relation field as optional in prisma schema.
How to reproduce
- Set delete behaviour to cascade in db:
ALTER TABLE public.Post
DROP CONSTRAINT Post_authorId_fkey,
ADD CONSTRAINT Post_authorId_fkey
FOREIGN KEY (authorId)
REFERENCES public.User (id)
ON DELETE CASCADE
ON UPDATE CASCADE;
- create at least one User and one Post that belongs to him.
- try to delete that User with prisma client
Expected behavior
The user and all his posts should be deleted.
Prisma information
schema:
model User {
id Int @id @default(autoincrement())
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
author User @relation(fields: [authorId], references: [id])
authorId Int
}
Environment & setup
DB: Postgres 11.4 Prisma: 2.0.0-beta.2 Node: 12.2.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:8
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Cascade Delete - EF Core
An optional relationship allows the blog to exist without an owner, which means cascade delete will no longer be configured by default.
Read more >EF Core One-to-One Optional Relation with Cascade Delete
In 1:n associations it seems more intuitive to us that the independent entity, the parent, doesn't have a foreign key to its children....
Read more >How to organize optional cascading deletes of foreign key ...
I want to make foreign key cascading delete for junction tables and add triggers to catch this deletes, then check if the main...
Read more >Referential actions
If you do not specify a referential action, Prisma uses a default. If you upgrade from a ... This might result in cascading...
Read more >Documentation: 15: 5.4. Constraints
Restricting and cascading deletes are the two most common options. RESTRICT prevents deletion of a referenced row. NO ACTION means that if any...
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 Free
Top 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
Please don’t say it’s not a bug. It is clearly documented that cascade delete should work if it is defined on the database level.
Changing all relations to optional would generate TS types as nullable and that means a lot of (previously unnecessary) null checking in TS.
Hopefully this can be fixed shortly, as it’s quite annoying.
Thanks for great work on prisma!
This is not a bug but by design. We enforce required relations on the Prisma layer. The schema clearly states that
Post.author
is required. The query engine finds a relatedPost
record for the user and hence rejects the change. I see two possible fixes:Post.author
to be optional for now. (as the title suggests)