New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No way to cascade delete when the foreign key is non-nullable #2057
Comments
I can confirm this. I think this is one the assumptions that query engine makes right now but I am not certain. |
Yes, I have exactly the same issue. I am running CREATE TABLE `platform`.`entity_basket_item` (
...
FOREIGN KEY (`entity_basket_id`) REFERENCES `platform`.`entity_basket` (`id`) ON DELETE CASCADE
) If the Foreign Key (
Then I receive error:
However making the Foreign Key nullable (
|
@robmurtagh By "fixes the error" do you mean cascade delete works? |
Yes, exactly, cascade delete then works as expected. |
Thanks @robmurtagh for the confirmation. Last time I tried doing that with beta-1, it didn't work for me. I'll try again. Do you mind sharing your prisma version? |
Yes, I'm on |
Hey @robmurtagh I tried your solution and it works. But ideally it should also work when the foreign key is non-nullable. |
This one can be probably closed now as well. |
Does that mean that the issue is fixed? Amazing news if so 😄 |
@dodas |
I am running beta 6 and the issue doesn't seem to be fixed. I would guess that the closed linked issue was included in the release notes by accident. |
Release notes unfortunately can not differentiate between issues that were closed as a duplicate vs. really fixed. |
This comment has been minimized.
This comment has been minimized.
This issue depends on the implementation of cascading deletes: #2810 Unfortunately, cascading deletes are not actively being worked on right now but they're already on the roadmap and will likely be picked up by the engineering team soon! In the meantime, I want to mention that @AhmedElywa has built an awesome library that brings you cascading deletes to Prisma already today: https://paljs.com/plugins/delete You can use it as a workaround for the time where it's not yet natively supported by Prisma. |
Ha funny that I didnt find it before, related issue: #4650 |
ON DELETE CASCADE is not used because it seems like not working See: prisma/prisma#2057
As a workaround, one could execute a raw sql statement through prisma so that the cascaded delete constraint set on the database would work as expected.
https://www.prisma.io/docs/concepts/components/prisma-client/raw-database-access |
Yes, and, that's what we're having to do, but, really, this sort of thing should not have to be worked around!!! |
I think it's a better workaround to recreate foreign key the right way after applying Prisma migration:
|
What's the status? This is obviously an important bug. I'm trying prisma and run into that issue. ORM's 🤗 |
According to the roadmap : https://www.notion.so/Prisma-Roadmap-50766227b779464ab98899accb98295f Prisma team is working on it |
Is this a priority at all for the Prisma team? seems like not 😢. Prisma even has documented the following solutions for cascading deletes as if they were the correct solution all along (not as workarounds).
|
Prisma team is working hard on a large bundle of tools like Prisma Studio or Prisma migrate. The latest was a mandatory step to achieve a stable and production-ready cascade delete. |
Hello from Prisma here! @jasonlimantoro Thanks, @MikaStark - It's true! The team is actively working on this problem. Right now, the potential design for this feature is being discussed and you can expect soon to see a proposal that will be shared with the community for broader feedback, before going into the implementation phase. Hope this helps alleviate some concerns around the timing and prioritization of this. |
I do like the current implementation with the deletion being forbidden on non-optional relations but that should be an optional kind of behaviour, the cascade deletes should work by default and we should then be able to optionally enforce strict no-cascade behaviour with a modifier on the relationship itself. That would be awesome. |
Hello everyone and thanks for your patience! We've spent some time brainstorming about problem, discussing possible solutions, and finally have a proposal to share. The idea is to extend the Prisma Schema Language in order to be able to encode Please find the proposal with all the relevant details in this GH issue. We are welcoming feedback at this stage and look forward to hearing from you! |
Fixed in prisma/prisma-engines#1947 Instructions on how to use this, and a place to give feedback: #7816 |
This has now been released as a preview feature behind a preview feature flag. You can read about it in the release notes for 2.26.0: https://github.com/prisma/prisma/releases/tag/2.26.0 If you have any feedback, please use this issue: #7816 |
Bug description
When the annotated relation field in schema.prisma is made mandatory (no
?
type suffix), running the migration automatically sets cascade deletion. When it is non-mandatory, it sets ON DELETE SET NULL. There should be a way to set the behavior in both cases.Even when using introspection, when the "one" side of the relation is made non-nullable, deleting a record on this side causes prisma query engine to throw a relation violation error. Cascade delete works when the "one" side is nullable
How to reproduce
Steps to reproduce the behavior:
new PrismaClient().user.delete({where: {id: 1}})
RelationViolation
errorExpected behavior
Environment & setup
The text was updated successfully, but these errors were encountered: