Delete of "parent" should not be prevented if it will cascade delete "children" on required relations
See original GitHub issueBug description
Consider the simplified schema:
model Calendar {
id String @id @default(cuid())
events CalendarEvent[]
}
model CalendarEvent {
calendar Calendar @relation(fields: [calendarId], references: [id])
calendarId String
id String @id @default(cuid())
}
When trying to delete calendar I get:
The change you are trying to make would violate the required relation 'CalendarToCalendarEvent' between the `Calendar` and `CalendarEvent` models.
at PrismaClientFetcher.request (/Users/<REDACTED>/node_modules/@prisma/client/runtime/index.js:78121:15)
at processTicksAndRejections (internal/process/task_queues.js:94:5) {
code: 'P2014',
clientVersion: '2.13.0',
meta: {
relation_name: 'CalendarToCalendarEvent',
model_a_name: 'Calendar',
model_b_name: 'CalendarEvent'
},
This is contrary to what the doc says on the matter https://www.prisma.io/docs/guides/general-guides/database-workflows/cascading-deletes/postgresql
How to reproduce
- Create the schema above
- Use the āoldā migration system to generate a migration
- Apply said migration
- Put some rows in both tables
- Try to delete calendar, you get the error
Expected behavior
You should be allowed to delete the calendar and let the DB delete the children objects. This is a somewhat new behaviour because I remember doing that successfully in a previous version.
Prisma information
I tested on Postgres, but probably is also valid for other databases.
@prisma/cli : 2.13.0
@prisma/client : 2.13.0
Current platform : darwin
Query Engine : query-engine 833ab05d2a20e822f6736a39a27de4fc8f6b3e49 (at ../node_modules/@prisma/engines/query-engine-darwin)
Migration Engine : migration-engine-cli 833ab05d2a20e822f6736a39a27de4fc8f6b3e49 (at ../node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 833ab05d2a20e822f6736a39a27de4fc8f6b3e49 (at ../node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 833ab05d2a20e822f6736a39a27de4fc8f6b3e49 (at ../node_modules/@prisma/engines/prisma-fmt-darwin)
Studio : 0.329.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:11
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Good explanation of cascade (ON DELETE/UPDATE) behavior
My first guess would be the Child records get deleted when Parent records are deleted, since Child records depend on Parent records, but...
Read more >Using the ON DELETE CASCADE Option - IBM
Use the ON DELETE CASCADE option to specify whether you want rows deleted in a child table when corresponding rows are deleted in...
Read more >Is anyone seeing a cascading delete even when the check ...
I have a simple script that deletes the active portal row. In the relationship diagram the relationships do not have either allow creation...
Read more >SQL Server: Foreign Keys with cascade delete - TechOnTheNet
A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the...
Read more >groovy - How to prevent from Grails not to delete child while ...
You can set cascade to only work on save-update but then when Author is delete there is constraint violation exception, because there is...
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
Thanks for the very helpful reproduction @Sytten! You can workaround this by making the Calendar optional:
We have a guide that discusses this in more detail: https://www.prisma.io/docs/guides/general-guides/database-workflows/cascading-deletes/postgresql
However, I donāt think the cascade settings shouldnāt depend on optionality. Iāll talk to the team about fixing this up.
Looks like this one is unfortunately blocked by #2810. We have this check to maintain data integrity when you have a required relation without cascade delete on the underlying database. It will also be needed for Mongo soon.
Since we donāt have state in the schema about the databaseās cascade settings, we canāt turn this check on and off accordingly. Weāll revisit this one after #2810.
In the meantime, optional relations or Ahmedās paljs plugin is the way to go. Sorry about that!