How to properly handle relationships and references

Right now i have this 3 files and to avoid leaving relationships hanging in my delete I manually go through each relationship and delete it. The reason I have the other 2 (update and create) setup the way they're is because I was following this tutorial from the docs, but it doesnt have anything on destroy, and just putting change manage_relationship(:ticket_ids, :tickets, type: :remove) fails because I am not passing the ticket_ids, now my question goes, what is the right way to manage both on DB and make ash_phoenix happy, should I manually setup references (so that its also handled at the DB level) should I just not touch the references part and leave it as is? I am not sure whats the correct way is. Normally I would do this operation (the on_delete and on_update) at the db level, but i am not sure how to do it here so that everyone plays nice together
Solution:
Just had to add
references do
reference :team, on_delete: :delete
reference :ticket, on_delete: :delete
end
references do
reference :team, on_delete: :delete
reference :ticket, on_delete: :delete
end
on the union/intermediary table, and then I can delete teh custom destroy action, heres the final files in case some needs them later...
Jump to solution
8 Replies
Solution
Vic
Vic2mo ago
Just had to add
references do
reference :team, on_delete: :delete
reference :ticket, on_delete: :delete
end
references do
reference :team, on_delete: :delete
reference :ticket, on_delete: :delete
end
on the union/intermediary table, and then I can delete teh custom destroy action, heres the final files in case some needs them later
Vic
VicOP2mo ago
In retrospect it does make sense, the many_to_many works to tell ash, and the has_many/belongs_to represent the DB implementation after all
barnabasj
barnabasj2mo ago
As a sidenote, there are also cascade_destroy and cascade_update changes https://hexdocs.pm/ash/Ash.Resource.Change.Builtins.html#cascade_destroy/2
Vic
VicOP2mo ago
Nice! Didn't know that one existed Just, one question, what would be the point of doing it that way? Does it integrate better with ash_paper_trail or some other tool in ash/elixir? Isnt it better to let the DB do it?
barnabasj
barnabasj2mo ago
sometimes you want to execute some kind of logic when a destroy happens, so your destroy action might have a after_action hook or something to do some sideeffect. that wouldn't be triggered if it's all done in the db
Vic
VicOP2mo ago
Say for example a broadcast action?
barnabasj
barnabasj2mo ago
yeah, could be notifications
Vic
VicOP2mo ago
Thanks!!

Did you find this page helpful?