typed_dag icon indicating copy to clipboard operation
typed_dag copied to clipboard

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

Open rguiscard opened this issue 4 years ago • 3 comments

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 !!

rguiscard avatar Apr 08 '21 07:04 rguiscard

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.

ulferts avatar Apr 08 '21 15:04 ulferts

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 !!

rguiscard avatar Apr 09 '21 02:04 rguiscard

@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

delphaber avatar Oct 08 '21 08:10 delphaber