Skip to content
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

Soft-delete cascade #9

Open
RuiPMoreira opened this issue Mar 1, 2018 · 5 comments
Open

Soft-delete cascade #9

RuiPMoreira opened this issue Mar 1, 2018 · 5 comments

Comments

@RuiPMoreira
Copy link

Hi, first of all, congratulations for this awesome plugin.

I'm trying to do soft delete applied to the parent model and them propagate this for every single child, and them for the childs of childs.
This is probably something that i'm doing wrong. But every thing is doing well with hardDelete.

Ty in advance,
Rui

@griffinpp
Copy link
Owner

@RuiPMoreira I'm not sure what you're trying to accomplish, maybe a code snippet would help clarify?

@RuiPMoreira
Copy link
Author

Imagine that you have this tables.

Parent {id} ---> Child{id, parent_id}

If you try to delete the father with id 1, all the childs with "parent_id=1" should be deleted.
That works really well in sql because you can define this foreign key as "ON DELETE CASCADE".

@griffinpp
Copy link
Owner

ok, you're referring to the actual CASCADE functionality of the db. from the point of view of the database, you're not deleting anything, you're just updating the value in a column, so you can't set up a cascade for that. you could either set up a trigger on the table(s) to handle this (which could get ugly), attempt to set up something in the afterUpdate lifecycle function for your model (better, but not something I've done, so ymmv), or else find ways to filter around it in code, which might be easier than you think. In practice, I generally set up any relations to tables that have soft delete functionality to automatically filter out any rows that are marked deleted (like the example here: https://github.com/griffinpp/objection-soft-delete#using-a-relationship-filter). That way, any thing that links to a parent record that gets deleted generally doesn't get access to any child records since the parent never shows up in the first place.

At any rate, these are all details of how you set up your particular implementation, and are not really something that the plugin can do right now.

@IAmBrandonMcGregor
Copy link

IAmBrandonMcGregor commented Oct 31, 2018

I encountered this limitation as well. Until this is officially supported, I'm using the $afterDelete hook plugin's lifecycle hook context.

@Makpoc
Copy link

Makpoc commented Feb 26, 2019

+1. Mind sharing some code that solves it using the lifecycle hooks?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants