zac

zac

Implementing cascading deletes in Ash Postgres

I am deeply confused by what’s going on.

  1. I have two entities – Sprint and ActivityStereotype. The former is the “parent,” meaning, sprints can have zero or more activity stereotypes. When deleting a sprint its activity stereotypes should also be deleted.
  2. It works fine in iex -S mix interactively. See below.
  3. But when I try to compile / run a unit test, I get a bizarre 42P07 “duplicate table” error.
  4. But I’m also a little bit confused about Ash architecture and how relationships (and specifically, cascading relationships in Postgres) are supposed to be handled.

Like I said, all is well interactively:

iex(6)> {:ok, s2} = COE.Walk.Sprint.create("meeting", ~D[2011-01-01], ~D[2011-01-15])
{:ok, #COE.Walk.Sprint...}
iex(7)> {:ok, a2} = COE.Walk.ActivityStereotype.add("meetings", s2.id)
{:ok, #COE.Walk.ActivityStereotype...}
iex(8)> s2
iex(9)> |> COE.Walk.destroy!()
:ok

My journey thus far…

I had assumed on_delete: was handled by Ash internally when using relationships with not null constraints. For example:

# activity_stereotype.ex
  relationships do
    # Activity stereotypes belonog to a sprint, so this is a required relationship
    belongs_to :sprint, COE.Walk.Sprint do
      attribute_writable? true
      allow_nil? false
    end
  end

# sprint.ex
  relationships do
    has_many :activity_stereotype, COE.Walk.ActivityStereotype
  end

Apparently not, which is fine. At this stage, deleting a sprint results in an error that “would leave records behind” (which I interpret to mean, “you can’t do that, it would leave an orphan activity stereotype”). Makes sense. Begin quest to figure out how to configure cascading deletes.

In trying to figure out how to set up a cascading delete, I found a relevant page on References in Ash Postgres. It’s lacks some context and complete examples, but… it seems to be saying I should add a new references block to my postgres block, so I added it here (footnote; kind of ugly that I now have two references in my module… seems redundant but meh)…

# activity_stereotype.ex
  postgres do
    table "stereotypes"
    repo BossSite.Repo

    references do
      reference :sprint, on_delete: :delete, on_update: :update, name: "activity_stereotype_sprint_fkey"
    end
  end

And rebuilt the database (it’s young, no need for migrations). Data rebuild is fine. All looks good.

Compile. Run unit tests.

Now I’m getting an error during unit tests that I really don’t understand:

% mix test test/coe/activity_stereotype_test.exs
Compiling 2 files (.ex)
** (Postgrex.Error) ERROR 42P07 (duplicate_table) relation "stereotypes" already exists
    (ecto_sql 3.11.0) lib/ecto/adapters/sql.ex:1054: Ecto.Adapters.SQL.raise_sql_call_error/1
    (elixir 1.15.7) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
    (ecto_sql 3.11.0) lib/ecto/adapters/sql.ex:1161: Ecto.Adapters.SQL.execute_ddl/4
    (ecto_sql 3.11.0) lib/ecto/migration/runner.ex:348: Ecto.Migration.Runner.log_and_execute_ddl/3
    (elixir 1.15.7) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
    (ecto_sql 3.11.0) lib/ecto/migration/runner.ex:311: Ecto.Migration.Runner.perform_operation/3
    (stdlib 5.0.2) timer.erl:270: :timer.tc/2
    (ecto_sql 3.11.0) lib/ecto/migration/runner.ex:25: Ecto.Migration.Runner.run/8

I checked the Postgres instance (and the migrations)… there is only one stereotypes table. I’m assuming this means there is some kind of duplicate reference being generated in the dynamic SQL somewhere… but… no idea. Drop into iex and play around to see if I can figure out what’s up:

It works just fine. WTH… (ノಠ益ಠ)ノ彡┻━┻

A few key points:

  • I’m getting the error at unit test compile (not when running migrations / creating the database; the database is created – this happens during unit testing only)
  • I’ve checked the SQL and it looks good to me, although my SQL is rusty – including it below just in case – anyhow, it works great in iex
  • After reading this post I’m more confused about Ash Postgres, relationships, and the idiomatic approach to cascading constraints… and am wondering if I should be adding on_missing: destroy to a manage_relationships call – because mostly, it sounds like @zachdaniel is saying “that should have been managed by Ash core anyhow.” I think. But this is just tangential and doesn’t really answer WTH is going on with my unit tests…!

For thoroughness… the migration:

# database migration
def up do
    create table(:stereotypes, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
      add :name, :text, null: false
      add :allowed_types, {:array, :text}, null: false
      add :default_type, :text, null: false, default: "value_add"
      add :sprint_id, :uuid, null: false
    end

    create table(:sprints, primary_key: false) do
      add :id, :uuid, null: false, default: fragment("uuid_generate_v4()"), primary_key: true
    end

    alter table(:stereotypes) do
      modify :sprint_id,
             references(:sprints,
               column: :id,
               name: "activity_stereotype_sprint_fkey",
               type: :uuid,
               prefix: "public",
               on_delete: :delete_all,
               on_update: :update_all
             )
    end

    alter table(:sprints) do
      add :name, :text, null: false
      add :start_date, :date, null: false
      add :end_date, :date, null: false
    end

As always thanks for your help!

Marked As Solved

zac

zac

OMG. I just hit post and then it occurred to me. There is a different instance of the database for testing, that I haven’t blown away. :confused:

Sorry for the bother!! Off to remember how to do that. Leaving the post though, maybe someone will find it helpful in the future.

Also Liked

zachdaniel

zachdaniel

Creator of Ash

The way you’ve done it is correct, using ash_postgres’s references to bake the behavior into the data layer :slight_smile: In this regard we lean on the data layer to support this feature. If you were using a data layer that didn’t have support for this, you’d have to hand-roll something in an after action hook. But thankfully postgres has our back :laughing:

Where Next?

Popular in Questions Top

siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New

Other popular topics Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43622 214
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement