Problem when delete with cascade
See original GitHub issueI 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:
- Created 3 years ago
- Comments:5
Top 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 >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
@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.
@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.