Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable cascading delete behaviour with onDelete: CASCADE #4711

Closed
nikolasburk opened this issue Jul 29, 2019 · 22 comments · Fixed by prisma/prisma-engines#1947
Closed

Enable cascading delete behaviour with onDelete: CASCADE #4711

nikolasburk opened this issue Jul 29, 2019 · 22 comments · Fixed by prisma/prisma-engines#1947
Labels
kind/feature A request for a new feature. team/client Issue for team Client. team/schema Issue for team Schema. topic: cascade topic: referential actions
Milestone

Comments

@nikolasburk
Copy link
Member

The behaviour for cascading deletes is specified here.

@donjo9
Copy link

donjo9 commented Aug 15, 2019

I am looking very much forward for this.
with this schema:

model User {
    id         String   @default(cuid()) @id @unique
    name       String
    email      String   @unique
    playercode String   @unique
    password   String
    updatedAt  DateTime @updatedAt
    createdAt  DateTime @default(now())
    ownTeam    Team? @relation(name: "TeamToOwner", onDelete: CASCADE)
    team       Team? @relation(name: "TeamToPlayer")
    teamInvitations TeamInvitation[]
}


model Team {
    id        String   @default(cuid()) @id @unique
    name      String
    shortname String
    teamcode  String   @unique
    updatedAt DateTime @updatedAt
    createdAt DateTime @default(now())
    points    Int      @default(value: 1000)
    owner     User     @relation(references: [id], name: "TeamToOwner")
    players   User[]   @relation(references: [id], name: "TeamToPlayer")
    playerInvitations TeamInvitation[]
}

model TeamInvitation {
    id  String @default(cuid()) @id @unique
    player User
    team Team
}

When ever i want to delete a user who owns a team, i have to delete the team first.

@nhuesmann
Copy link

Hey guys, I just wanted to check on the status of this feature. I'm having trouble right now as I have 2 models with a 1 to 1 relation, and when I delete 1 I want it to delete the other. My schema is this:

model User {
  id             ID
  gameLibrary    UserGameLibrary
}

model UserGameLibrary {
  id        ID
  user      USER
}

Currently, I can't delete a User or UserGameLibrary — I get Error in connector: Violating a relation UserToUserGameLibrary between User and UserGameLibrary. Is there a way to achieve this functionality while we wait for cascading deletes?

@omarragi
Copy link

Did you solve this ?

@jkb-kt
Copy link

jkb-kt commented Nov 14, 2019

This is a really important one. Can it be prioritized? Where can we expect this feature?

@reinvanimschoot
Copy link

I feel this is a really important feature since it is a fundamental requirement for any mature project with lots of relations. I really hope this gets solved soon.

@kripod
Copy link
Contributor

kripod commented Dec 9, 2019

In the meantime, has anyone found a solution to delete records with relationships? I always encounter DB constraint failures.

My schema is as follows:

model Quiz {
  id                 String     @id @default(cuid())
  title              String
  items              QuizItem[]
  // ...
}

model QuizItem {
  id   String       @id @default(cuid())
  quiz Quiz         @relation(onDelete: CASCADE)
  // ...
}

Unfortunately, even when I try to delete each QuizItem of a corresponding Quiz, a constraint failure is raised.

@janpio janpio transferred this issue from prisma/prisma Dec 13, 2019
@Errorname
Copy link
Contributor

@kripod I think your relation is the wrong way around. You should put the @relation(onDelete: CASCADE) on the items field in Quiz, so that when a Quiz is deleted, it deletes the items on cascade

@reinvanimschoot
Copy link

Is there any update on this? This is currently the one big feature that is keeping me from moving some of my projects to Prisma2.

@mavilein
Copy link
Member

We will work on this once we focus on Prisma Migrate (formerly Lift). Right now we are focusing on Prisma Client (formerly Photon). Migrate will be our next focus.

@arvindell
Copy link

Hopefully this feature can be prioritized, it's quite a limitation for me 😕

@nikolasburk
Copy link
Member Author

nikolasburk commented Mar 24, 2020

Hey @AlexVilchis, while cascading deletes aren't supported by Prisma Migrate, you can still use them with Prisma by configuring them manually in the DB. We have a guide for that here (note that this guide is in the new docs which are still WIP, except pretty much all links to be broken and UX currently also isn't great – will improve over the next few days 🙂 )

@arvindell
Copy link

arvindell commented Mar 24, 2020

Hey @AlexVilchis, while cascading deletes aren't supported by Prisma Migrate, you can still use them with Prisma by configuring them manually in the DB. We have a guide for that here (note that this guide is in the new docs which are still WIP, except pretty much all links to be broken and UX currently also isn't great – will improve over the next few days 🙂 )

Great, I was looking for something like this. Thank you

@somonek
Copy link

somonek commented Mar 25, 2020

Hey @AlexVilchis, while cascading deletes aren't supported by Prisma Migrate, you can still use them with Prisma by configuring them manually in the DB. We have a guide for that here (note that this guide is in the new docs which are still WIP, except pretty much all links to be broken and UX currently also isn't great – will improve over the next few days 🙂 )

@nikolasburk the link to the guide is broken. Do you have it in any other form or a working link somewhere? I'm also looking for that. Would be a great workaround.

Thanks

@janpio
Copy link
Member

janpio commented Mar 25, 2020

I updated the link in @nikolasburk 's post to the new URL @somonek: https://prisma2.netlify.com/guides/database-workflows/cascading-deletes/postgresql

@aakashkag
Copy link

aakashkag commented May 10, 2020

Looking forward to see CASCADE feature . It's very important feature for any project ^^

@yovanoc
Copy link

yovanoc commented May 15, 2020

waiting for this too

@saudraisn
Copy link

Very important feature indeed!
Thanks for the good work you're doing though!

@Mattadore
Copy link

Same, I appreciate all the work but I'm currently migrating a project to Prisma2 and was shocked to find this hadn't yet been carried over from Prisma1, seems pretty fundamental for most nontrivial use cases.

@nikolasburk
Copy link
Member Author

I just want to quickly mention that while cascading deletes are not natively supported by Prisma, you can use @AhmedElywa's awesome library as a workaround that brings you cascading deletes to Prisma already today: https://paljs.com/plugins/delete

@pimeys
Copy link
Contributor

pimeys commented Jun 2, 2021

For the client implementation, the WIP branch with changes in ME/IE is in prisma/prisma-engines#1947

@janpio
Copy link
Member

janpio commented Jun 22, 2021

Instructions on how to use this, and a place to give feedback: #7816

@Jolg42 Jolg42 added this to the 2.26.0 milestone Jun 28, 2021
@janpio
Copy link
Member

janpio commented Jun 29, 2021

This has now been released as a preview feature behind a preview feature flag. You can read about it in the release notes for 2.26.0: https://github.com/prisma/prisma/releases/tag/2.26.0 If you have any feedback, please use this issue: #7816

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/client Issue for team Client. team/schema Issue for team Schema. topic: cascade topic: referential actions
Projects
None yet
Development

Successfully merging a pull request may close this issue.