Giter Site home page Giter Site logo

Discussion: Cascading Deletes about prisma1 HOT 19 CLOSED

prisma avatar prisma commented on April 30, 2024 19
Discussion: Cascading Deletes

from prisma1.

Comments (19)

danmkent avatar danmkent commented on April 30, 2024 8

My ideal way for this to be implemented would be:

  • Enable us to mark a one-one or one-many relation as a 'belongs to' relation where the second entity is dependent on the first

  • When the first entity (parent) in the relation is deleted, automatically delete the second entity (child) also following any further 'belongs to' relations and deleting further child entities if required

  • If a child entity is removed from its relationship with the parent entity, delete it

  • If a child entity is added to a relationship with a new parent, it can continue to exist

  • do not fire functions attached to the delete mutation of entities as this is an internal action rather than a mutation through the API

from prisma1.

kbrandwijk avatar kbrandwijk commented on April 30, 2024 1

Look at how SQL Server (or another database) implements cascade functionality. This kind of thing has been thought of, a lot, already...

from prisma1.

sorenbs avatar sorenbs commented on April 30, 2024 1

Thanks for the input everyone! I have written up a proposal here: https://github.com/graphcool/framework/issues/1262

It is a traditional approach inspired by cascading referential integrity constraints from SQL servers. Specifically it does not specify a belongs to relationship with all the If a child entity is removed from its relationship with the parent entity, delete it property described by @danmkent in https://github.com/graphcool/framework/issues/47#issuecomment-316421924 as I think this particular case is better handled by https://github.com/graphcool/framework/issues/1160

Please have a look and add comments in the new issue

from prisma1.

emolr avatar emolr commented on April 30, 2024

Would it make sense to also make it possible with cascading update?
I have posts on a Campaign, and i do "soft deletion". Like i'm not deleting the content if the user regrets later on, so i have a field called deleted on the Post model.

It might be me overcomplicating it myself. So if you anyone has a tip on how to properly handle soft deletion, feedback on my approach would also be nice :)

from prisma1.

marktani avatar marktani commented on April 30, 2024

@emolr how is the cascading update coming in here? Wouldn't your use case be handled by simply updating the delete field of a post?

from prisma1.

emolr avatar emolr commented on April 30, 2024

@marktani It's coming when i'm "soft deleting" a campaign, and all related posts should then be too :)

from prisma1.

sorenbs avatar sorenbs commented on April 30, 2024

We should consider what happens if a DELETE mutation callback is set up for the related model where many nodes are being deleted. Performing the actual deletion is fast, but performing potentially millions of mutation callbacks is expensive

from prisma1.

jvbianchi avatar jvbianchi commented on April 30, 2024

You could create a new type of mutation callback for this kind of operation.

from prisma1.

mkozhukharenko avatar mkozhukharenko commented on April 30, 2024

what is a solution for now? E.g. I have Course and it have, e.g. 50 Steps. When I'm deleting the course I dont want to make 51 request (deleting 50 Steps).

I propose to just t construct a dynamic query with all 50 mutations instead:

const client = new Lokka(...);
var allStepsIds = ['1', '2', '3', '4']
client.mutate(`{
    deleteStep: ${allStepsIds.map(id) => "deleteStep(id:" + id + "){ id }") }
}`).then(response => {
    console.log(response);
});

It seems that neither Apollo not batchql does not support mutation batching.

from prisma1.

sorenbs avatar sorenbs commented on April 30, 2024

This sounds very reasonable to me @danmkent

Would you fire the attached function on the "parent" entity - the one being explicitly deleted?
If so, would you provide extra information about deleted "child" entities?

from prisma1.

davidjpetersen avatar davidjpetersen commented on April 30, 2024

+1 for creating the cascading delete feature.

from prisma1.

kbrandwijk avatar kbrandwijk commented on April 30, 2024

Cascading functionality requires triggering an action. If the bugs/features for triggering SSS and/or RP on relation updates are implemented, then you're free to deal with this yourself.
Deleting a parent should trigger an update SSS for the children (not RP, because SSS is async, see performance comment from @sorenbs above), because the relation field is updated. UpdatedFields tells you what was changed, so it's very easy to create a cascase soft-delete/delete/update mutation for any use case imaginable.
You wouldn't declaratively be able to specify cascade behavior, but at least it would give you the triggers to easily handle anything based on it.
I'm not saying it wouldn't be nice to get this out of the box, but I'd rather get the building blocks, than a black box implementation that might or might not fit everyone's use cases...

from prisma1.

danmkent avatar danmkent commented on April 30, 2024

I think there is a good argument for providing two things:

  • A simple 'belongs to' semantic in the schema that manages the basic use case of having objects that should only live as long as their parent does

  • More options for server side subscriptions that can react to relationship changes (e.g. a child losing its relationship with a parent because it has been deleted) and can be used to implement specific requirements

I can understand that the simple use case may not fit everyone's use case but at the same time, having to add a server side subscription for every type and potentially triggering a cascade of thousands (or even more!) of separate server side subscriptions for a common use case that could be dealt with more efficiently seems like a bad idea.

from prisma1.

danmkent avatar danmkent commented on April 30, 2024

@sorenbs Yes, I would expect functions attached to the parent entity to be triggered as normal.

Personally, I would only use a cascading delete for child entities that don't have any existence beyond being part of the parent, so I can't think of a circumstance in which I would need details about them being deleted. If a child type was important enough to need a function attaching to it, I wouldn't be deleting it automatically in this way.

from prisma1.

typeofgraphic avatar typeofgraphic commented on April 30, 2024

There appears to be a need to differentiate the enforcement of a relation for data entry (e.g. a required one-to-many relation from User to Posts) from delete mutations. I've come across an use case with an app I'm building where there is a chain of relations across 3-4 Types.

For instance, if deleting a User deletes all his/her Posts in a cascading delete, what about when each Post has Comments, and those Comments have Votes? If these are all required, then would a cascading delete rip out everything in the relation chain?

In an app where multiple types are tied together in a chain of relations, having more specific semantic relation 'types' such as "belongs to" could dictate the depth of the deletion cascade. Developers need to feel confident that deleting a User won't rip out a trail of content or data.

It may be the case, for example, that a user is deleted, but that their Posts remain (e.g. archived).

The way around this is to make the relation optional (which is what I have done at this point) so as to make deletions far simpler. However, there are obvious drawbacks to this.

from prisma1.

nickluger avatar nickluger commented on April 30, 2024

This is the only thing that prevents me to switching to (the otherwise terrific !) graphcool currently. I assumed that Object Composition would be an extremly common concept in almost any product. If a "Book" has 50 "Chapters" how can in expect the client to delete them beforehand. Also i think this whole cascade should be (optionally) opaque to the client. Any news on this? How are you writing your apps currently when you need composition?

from prisma1.

kbrandwijk avatar kbrandwijk commented on April 30, 2024

You can implement your own cascade delete functionality using resolver functions. It's a bit of a stretch, but at least you have full control if you want to orphan/delete child nodes n levels deep.

from prisma1.

danstepanov avatar danstepanov commented on April 30, 2024

Any idea on when this might be implemented?

from prisma1.

sorenbs avatar sorenbs commented on April 30, 2024

@emolr I think the ability to update multiple nodes in a single mutation is what you want. See https://github.com/graphcool/framework/issues/81 :-)

from prisma1.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.