Is it good to have "on_delete: :cascade" on edge foreign_key ?
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 !!
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.
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
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