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.

Problem when delete with cascade

See original GitHub issue

I have two models

model Sale {
  id            String    @id @default(cuid())
  cart          Item[]
}

model Item {
  id                    String  @id @default(cuid())
  name              String
  sale                Sale     @relation(fields: [saleId], references: [id])
  saleId             String
}

Looking at show create table item on Mysql, I do have

  CONSTRAINT `item_ibfk_2` FOREIGN KEY (`saleId`) REFERENCES `Sale` (`id`) ON DELETE CASCADE ON UPDATE CASCADE

If I do delete from sale where id='some id' it deletes the sale and the items as expected.

But, when I do the same in Prisma prisma.sale.delete({ where: { id: 'some id' } }) I got the error bellow

Invalid `prisma.sale.delete()` invocation in webpack-internal:///./graphql/customs/MutationSale.js:459:38 The change you are trying to make would violate the required relation 'SaleToItem' between the `Sale` and `Item` models.

Am I doing something wrong?

Environment & setup

  • OS: Mac OS
  • Database: MySQL 5.7
  • Node.js version: 14.9.0
  • Prisma version: 2.9.0

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
P4sca1commented, Oct 26, 2020

@heybenchen Yes, this is actually a bug, so it does not work like explained in the docs. See https://github.com/prisma/prisma/issues/2057 for more info.

0reactions
ryands17commented, Oct 27, 2020

@heybenchen if your foreign key is nullable, then cascade deletes will work fine. It just doesn’t work in the issue that @P4sca1 has described above. As per the tutorial, the foreign key is nullable on the Post model so cascade deletes should work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Disadvantages to using ON DELETE CASCADE on every ...
A thing to keep in mind when using cascades is that it can cause conflicts if there's multiple cascade paths. SQL Server and...
Read more >
What is the disadvantages to using ON DELETE CASCADE ...
When you delete a row from the Parent table all the foreign key rows are deleted; This is usually faster than implementing this...
Read more >
Why you should avoid CascadeType.REMOVE for to-many ...
Most developers worry about deleting too many database records when they use CascadeType.REMOVE. And that's definitely an issue. But it's not the only...
Read more >
Adventures In Foreign Keys 3: Why Cascading Deletes ...
Users (Id) ON DELETE CASCADE;. These all get added without a problem. What if we delete some data? Harken Back, Ye Merry Reader....
Read more >
MySQL - ON DELETE CASCADE Constraint - GeeksforGeeks
ON DELETE CASCADE constraint is used in MySQL to delete the rows from the child table automatically, when the rows from the parent...
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