mindok

mindok

We have an Ash resource defined as polymorphic:

defmodule  MyApp.Location do

  use Ash.Resource, data_layer: AshPostgres.DataLayer

  postgres do
    repo MyApp.Repo
    polymorphic? true
  end

  attributes do
    uuid_primary_key :id
    attribute :resource_id, :uuid do
      allow_nil? false
      private? true
    end
    attribute :x, :float
    attribute :y, :float
  end

  ...

That is then used by 2 or 3 other resources, e.g.

defmodule  MyApp.Event do

  use Ash.Resource, data_layer: AshPostgres.DataLayer

  postgres do
    repo MyApp.Repo
    table "events"
  end

  attributes do
    uuid_primary_key :id
    attribute :when, :utc_datetime, allow_nil?: false, default: &DateTime.utc_now/0
    attribute :what, :string
    attribute :who, :string
  end

  relationships do
    has_one :location, Location do
      relationship_context %{data_layer: %{table: "event_locations"}}
      destination_attribute :resource_id
    end
  end

Is it possible to use the references config block somehow to delete Locations when the owning Event is deleted?

If it wasn’t polymorphic, we could do something like this in the Location resource, but given it references a specific relationship it won’t work with a polymorphic resource:

  postgres do
    repo MyApp.Repo
    # polymorphic? true

    references do
      reference :event, on_delete: :delete
    end
  end

The obvious (to me) alternative is setting up a destroy action on the Event resource that deletes the associated Location first, but then that has to be repeated for every other resource that may use Location which kinda defeats the purpose of using a polymorphic resource in the first place.

Edit: The destroy action isn’t so easy to get right either - how do you inject the datalayer[:table] context to ensure the delete is applied to the right table?

Showing Posts 1 to 7

zachdaniel

zachdaniel

Creator of Ash

You can do this to configure the polymorphic relationship.

references do
  polymorphic_on_delete :delete
end
mindok

mindok OP

That sounds straightforward. So I added that to the Location resource (the polymorphic one), and generated the migrations to update the relationship options in Postgres and no migration was generated:

“No changes detected, so no migrations or snapshots have been created.”

I’ll have a bit more of a play with breaking and recreating the relationships.

Edit: Looks like it needed to go on the opposite side of the relationship, i.e. on the Event resource.

zachdaniel

zachdaniel

Creator of Ash

That…doesn’t seem right. I can see where it’s doing that, but it doesn’t really make sense to configure that on the other resource. I think that might be a bug.

zachdaniel

zachdaniel

Creator of Ash

Can you open an issue for this in ash? We’ll have to wait until 3.0 to fix it since its a breaking change.

mindok

mindok OP

Will do.

There are probably some angles to consider. It makes sense to have it on the current side of the relationship if you want to control delete behaviour on a per relationship basis (i.e. Event → Location you may want the “cascade delete”, but Person → Location you may not). However, if you have relationships to two different polymorphic resources, the current approach doesn’t give enough control for per-relationship behaviour anyway.

If it’s on a per polymorphic resource basis it would make sense to swap it around.

zachdaniel

zachdaniel

Creator of Ash

Yeah, thats an good point. Regardless of which side you’re configuring it on you’d want to configure it per polymorphic relationship also. Probably needs to just be reworked in general.

mindok

mindok OP

It’s in the GH issue - on reflection, this seems to make sense to me given that the implementation details (e.g. table name) are given at this point:

defmodule MyApp.Event do
  ...
  relationships do
    has_one :location, Location do
       # Data layer context captures implementation details
      relationship_context %{data_layer: %{table: "event_locations", on_delete: :delete}}
       # or relationship_context %{data_layer: %{table: "event_locations"}, on_delete: :delete}
      destination_attribute :resource_id
    end
  end

...
— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews