Ash FrameworkAF
Ash Framework6mo ago
6 replies
AngryBadger

Managing Relationships - Cascading deletes for has_one relationships.

I'm trying to work through setting up relationships the Ash-way and here's roughly the structure I have:
* A Campaign
Has One Ship
Has One Stash
** Has Many Crew Members

I have set up my campaign resource like this:
def MyApp.Campaigns.Campaign do
  #...
  postgres do
    #...
    references do
      reference :stash, on_delete: :delete
      reference :ship, on_delete: :delete
    end
  end

  actions do
    create :create do
      #...
      change manage_relationship(:stash, type: :direct_control)
      change manage_relationship(:ship, type: :direct_control)
    end
    # ... other actions
    destroy :destroy do
      accept [:id]

      change cascade_destroy(:stash, action: :destroy)
      change cascade_destroy(:ship, action: :destroy)
    end
  end

  relationships do
    # ... other relationships
    has_one :stash, FiveApps.Campaigns.Stash
    has_one :ship, FiveApps.Campaigns.Ship
  end
end
Was this page helpful?