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

Is it good to have "on_delete: :cascade" on edge foreign_key ? #41

Open
rguiscard opened this issue Apr 8, 2021 · 3 comments
Open

Is it good to have "on_delete: :cascade" on edge foreign_key ? #41

rguiscard opened this issue Apr 8, 2021 · 3 comments

Comments

@rguiscard
Copy link

It is often to have "on_delete: :cascade" on association. In typed_dag, it would be on the edge AR.
This is not mentioned in the README. I wonder whether it is a good practice or not to add "on_delete: :cascade" on edge migration like this:

    add_foreign_key :edges, :nodes, column: :from_id, on_delete: :cascade
    add_foreign_key :edges, :nodes, column: :to_id, on_delete: :cascade

Thanks !!

@ulferts
Copy link
Collaborator

ulferts commented Apr 8, 2021

Admittedly I haven't thought about it before and thus haven't tested it.

I'd assume that having a cascade on the edges is ok.

What is not supported however, is having anything that deletes the edge database records without going through Rails. So a delete on the edge class or an on_delete: :cascade on anything referencing the edges will break the DAG.

@rguiscard
Copy link
Author

rguiscard commented Apr 9, 2021

Could you please elaborate on deleting edges through Rails ? If I destroy a node in Rails via node.destroy, do I also need to destroy edges manually, e.g. node.descendants.destroy_all, or typed_dag will handle the destruction of edges internally ? Thanks !!

@delphaber
Copy link

@rguiscard

or typed_dag will handle the destruction of edges internally?

Yes I think this is the case. You don't have to run node.descendants.destroy_all

What @ulferts says is that you should always delete through Rails using, for example, node.destroy and not node.delete. node.delete will run a direct SQL DELETE statement and typed_dag callbacks won't be called in this case. So the DAG will break

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

No branches or pull requests

3 participants