Cascading delete and pub_sub

Hi @everyone, I have a resource (Notification) ```elixir defmodule App.Notifications.Notification do use Ash.Resource, .... postgres do table "notifications" repo CauseBeacon.Repo references do .... reference :message, on_delete: :delete, index?: false .... end end pub_sub do module CauseBeaconWeb.Endpoint prefix "notifications" transform fn pub_sub_notification -> Map.take(pub_sub_notification.data, [:id, :user_id, :message_id, :activity_id]) end publish :create, ["created", :user_id] publish :mark_as_read, ["updated", :user_id, :id] publish :mark_as_unread, ["updated", :user_id, :id] publish :destroy, ["destroyed", :user_id, :id] end relationships do ... belongs_to :message, CauseBeacon.Notifications.Message, allow_nil?: true, public?: true end ... end When I delete a Message, it also deletes the Notification. BUT it seems to not generate the pub_sube notification. Did I forget something specific in my code ? Regards, Angy.
Solution:
anytime 🙂
Jump to solution
7 Replies
Rebecca Le
Rebecca Le•2mo ago
this is because the destroy is being handled by the database via a database cascade - not by calling the destroy action of the resource to resolve this, you'll have to remove the database cascade and use something like https://hexdocs.pm/ash/Ash.Resource.Change.Builtins.html#cascade_destroy/2 in the destroy action for your Messages
AngyL75
AngyL75OP•2mo ago
I am trying to make it work in the Notification resource I did:
postgres do
table "notifications"
repo App.Repo

references do
# Note, we do not do `on_delete: :delete` in order for pub_sub to occur on destroy from messages
reference :message, index?: false
end
end
postgres do
table "notifications"
repo App.Repo

references do
# Note, we do not do `on_delete: :delete` in order for pub_sub to occur on destroy from messages
reference :message, index?: false
end
end
in the Message resource I did:
destroy :hard_destroy do
soft? false
# Note: when hard destroying a message, we should also 'cascade-destroy' the notifications too, before action,
# we could do it in the DB ( setting up reference ) but pub_sub would not be generated in notifications resource.
cascade_destroy(:notifications, action: :destroy, after_action?: false)
end
destroy :hard_destroy do
soft? false
# Note: when hard destroying a message, we should also 'cascade-destroy' the notifications too, before action,
# we could do it in the DB ( setting up reference ) but pub_sub would not be generated in notifications resource.
cascade_destroy(:notifications, action: :destroy, after_action?: false)
end
But then it fails when trying to hard destroy a message. I have the following message:
Failed to delete message:[%Ash.Error.Changes.InvalidAttribute{field: :id, message: "would leave records behind", private_vars: [constraint: "notifications_message_id_fkey", constraint_type: :foreign_key, detail: "Key (id)=(57eaac04-b892-4cce-8403-d6745950961b) is still referenced from table "notifications"."], value: nil, has_value?: false, splode: Ash.Error, bread_crumbs: ["Error returned from: CauseBeacon.Notifications.Message.hard_destroy"], vars: [], path: [], stacktrace: #splode.Stacktrace<>, class: :invalid}]
Failed to delete message:[%Ash.Error.Changes.InvalidAttribute{field: :id, message: "would leave records behind", private_vars: [constraint: "notifications_message_id_fkey", constraint_type: :foreign_key, detail: "Key (id)=(57eaac04-b892-4cce-8403-d6745950961b) is still referenced from table "notifications"."], value: nil, has_value?: false, splode: Ash.Error, bread_crumbs: ["Error returned from: CauseBeacon.Notifications.Message.hard_destroy"], vars: [], path: [], stacktrace: #splode.Stacktrace<>, class: :invalid}]
@Rebecca Le, i did the
after_action?: false
after_action?: false
in order for the deletion of the notification to be before deletion of the message. what do I do wrong ?
Rebecca Le
Rebecca Le•2mo ago
that looks correct to me oh you're missing the change before cascade_destroy
AngyL75
AngyL75OP•2mo ago
AHAHAHA thanks, hard destroy works now.... but still no pub_sub generated by Notifications resources...
Rebecca Le
Rebecca Le•2mo ago
you also need return_notifications?: true for the cascade_destroy and yeah I've made that same mistake a few times 😅
AngyL75
AngyL75OP•2mo ago
All working now 🙂 thanks. that was really helpful. OH, I just realize it was all written in the
ash-framework_v6.pdf
ash-framework_v6.pdf
book!
Solution
Rebecca Le
Rebecca Le•2mo ago
anytime 🙂

Did you find this page helpful?