How to cascade delete properly in a self-relation with mongodb ?
It is written in the doc that we need to set NoAction on self-relation database but how are we supposed to do it ?
I tried getting all ids and using a deleteMany to delete parent and children together but I got a PrismaClientKnownRequestError.
15 Replies
I let this thread open, I used the run raw query but I would like something cleaner if someone has
Hi :vmathi: You could try this:
My tags are recursive, do you mean that I should create a query per level ?
I remember that using deleteMany already creates a transaction, what's new with this way ?
What do you mean recursive?
it means that a tag can have children, and these children may also have children, etc
Children can have children? 🤔
A tag can have children tags, and these tags can also have children tags
That's why I created a recursive function to get all the children ids
Oh, something like that?
Not sure how you managed that, but my best guess would be to recommend you to use the
on delete cascade
and so, if one is deleted, the others are also deleted
By doing that, you only need to specify 1 tag id and all its children will be deletedyes it's something like that
I just used a childrenId like in the model above, the on
on delete cascade
is not authorized on mongodb, that's the issue hereSpecial rules for referential actions in SQL Server and MongoDB | P...
Circular references or multiple cascade paths can cause validation errors on Microsoft SQL Server and MongoDB. Since the database does not handle these situations out of the box, learn how to solve this problem.
Did you fix it?
How can I fix it ?
For now I used a raw query which disables prisma verifications
Well, if you're using Mongo, it should be an object. If you delete the parent object, it should delete everything
If I use objects it means that I should normalize on the front-end or the back-end, and if I want to modify a child element I need to search in the elements or modify the whole object
I know that I'm trying to use a document database for relational purposes but most of my data is in document format except this part
Also it means that I should duplice all my fields into a schema and a type