Prisma-client-js: .deleteMany() causes panic, .delete() fails silently, .raw() delete works

Created on 23 Mar 2020  路  11Comments  路  Source: prisma/prisma-client-js

Since cascade deletes are not yet supported, we have added some handling to delete a set of related tables in order.

When deleting one of the tables (Person, below), a deleteMany causes a Panic, and a deleteOne fails silently (succeeds without deleting the row). A raw delete query works.

We believe this likely has something to do with the relation handling, but the error seems to be on the Person-User relationship. The actual schema has additional relationships, but the listed relationships seem to be causing the problem.

The concerning part to us was the silent failure on the delete - we expected this to error if there was an issue.

model Person {
  id        Int         @default(autoincrement()) @id
  student   Student?
  parent    Parent? 
  user      User
}
model Parent {
  id               Int            @default(autoincrement()) @id
  person           Person
  students          Student[]
}
model Student {
  id            Int            @default(autoincrement()) @id
  parent        Parent
  person        Person
}
model User {
  id                       Int                        @default(autoincrement()) @id
  person                   Person?
}

We have a replication example that we will send directly.

bu2-confirmed kinbug tecengines

Most helpful comment

@dpetrick You're correct about the deletion order. The main issue was around error handling - the panic and silent failures. If you are now getting relationship constraint violation errors thrown, that would be the fix.

All 11 comments

Can you help us reproduce this with the schema here maybe? Best would be to have the underlying SQL and a super simple script using Prisma Client that shows what exactly you are executing. That would enable us to reproduce the problem much quicker.

Please refer to the separate github repo I've shared for full replication. Let me know if I need to add anyone.

The link was https://github.com/Linktheoriginal/prisma-nexus-n1-example
Please invite @pantharshit00 if he is not already so he can take a look.

He should already have access. I can help with any replication issues via slack.

@Linktheoriginal I can reproduce the deleteMany error in your setup but I wasn't able to find a minimal reproduction. So this will need some more investigation from the backend team

Regarding the N+1 problem you have mentioned in the repository, it is related to https://github.com/prisma/prisma-client-js/issues/562 so wait on that to be fixed.

Internal link for more details: https://github.com/prisma/prisma2-private/issues/31

Before diving deeper into this, I suggest an update to the latest beta and with it the overhauled relation definitions. This might already be resolved.

I could find a reproduction with the latest alpha where both delete and deleteMany fail silently

https://github.com/divyendu-test/prisma-client-js-600

I can confirm the reproduction posted by @divyenduz

I looked into the issue and I don't quite understand what the actual problem is. In both cases I get

"The change you are trying to make would violate the required relation 'PersonToStudent' between the `Person` and `Student` models."

which is exactly how it's supposed to be.

You have required relations to the Person model, and because there are records existing that point to the record you're trying to delete, it will fail.

Of course the raw API works, but you're left with invalid data because you bypassed the checks Prisma does to guarantee data integrity.

This is a shortcoming on our end in a sense that we need cascade support, but the behavior seen here in the issue is intended. IF you want to implement cascades yourself you need to start deleting from the non-required end upwards, not the other way around.

@dpetrick You're correct about the deletion order. The main issue was around error handling - the panic and silent failures. If you are now getting relationship constraint violation errors thrown, that would be the fix.

Thanks for the feedback, appreciate the report.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

samrith-s picture samrith-s  路  3Comments

maartenraes picture maartenraes  路  4Comments

esistgut picture esistgut  路  4Comments

Errorname picture Errorname  路  3Comments

MichalLytek picture MichalLytek  路  3Comments