question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cascade delete does not work unless relation is optional

See original GitHub issue

Bug 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

  1. 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;
  1. create at least one User and one Post that belongs to him.
  2. 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:closed
  • Created 3 years ago
  • Reactions:8
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

19reactions
dodascommented, May 1, 2020

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!

6reactions
mavileincommented, Apr 22, 2020

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 related Post record for the user and hence rejects the change. I see two possible fixes:

  1. You could change Post.author to be optional for now. (as the title suggests)
  2. The real long term fix is to implement cascading deletes. Then the information about cascading deletes would be present in the schema and the query engine could act accordingly. This is tracked here.
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found