How to delete One-to-many self relations in Prisma?
I define the discussion model as a 1-to-m self-relationship.
But something goes wrong when I delete parent discussions (discussions with many replies).
It's totally fine if I delete the discussion without any reply.
How can I properly modify or delete? Thanks!
8 Replies
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Thank for reply! I'm just learning Prisma too (Even SQL database)
I have read https://www.prisma.io/docs/concepts/components/prisma-schema/relations/self-relations#one-to-many-self-relations
And i think the child or parent is just different in name and update order (if i'm wrong can you give me more specific documentation? thanks)
If I specify onDelete other than NoAction, the prisma extension suggested to me is wrong
Btw, I ran raw query Delete and it was fine.
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Thanks for the idea. Now i do it in raw query combine with delete many child Discussions.
I don't know if it affects performance but it works.
I will try your idea later! Thanks a lot!
If this works for you great, but I would be be careful with raw queries, especially if your still learning. Your giving up a lot of the benefits of prisma (the type safety) by using them. It’s mainly meant for highly optimized complex queries, not simple delete queries.
I tend to update first, delete or disconnect and then delete. I’m also a beginner though so if anyone else has a better way then by all means follow their advice!
if you soft delete something, you dont necessarily need the row and all their children
but if you really want to delete the row, you have to check how you will handle the children, if you will set their relationship as null, will remove them or whatnot