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.

Delete of "parent" should not be prevented if it will cascade delete "children" on required relations

See original GitHub issue

Bug 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

  1. Create the schema above
  2. Use the ā€œoldā€ migration system to generate a migration
  3. Apply said migration
  4. Put some rows in both tables
  5. 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:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

4reactions
matthewmuellercommented, Jan 15, 2021

Thanks for the very helpful reproduction @Sytten! You can workaround this by making the Calendar optional:

model Calendar {
  id                   String                        @id @default(cuid())
  events          CalendarEvent[]
}

model CalendarEvent {
- calendar     Calendar @relation(fields: [calendarId], references: [id])
+ calendar     Calendar? @relation(fields: [calendarId], references: [id])
  calendarId   String
  id           String   @id @default(cuid())
}

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.

3reactions
matthewmuellercommented, Jan 15, 2021

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!

Read more comments on GitHub >

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

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