Hi all,
I have a Item resource that has a many_to_many relationship with Category resource through ItemCategory resource.
I can create, update and read, but I am getting error: “would leave records behind” when trying to delete an Item or a Category. What’s the proper approach to deleting in this case?
Thanks. Where in the resouce should I put it?
Here is an example: DSL: AshPostgres.DataLayer — ash_postgres v2.0.1
You will want to configure the join resource’s references to tell the system to delete the joins when their :item or :category is deleted.
1 Like
It is not clear whether the references are added within relationships block in a resource and whether this requires to generate a new migration.
Also it looks like I am missing a module import or requirement, but I can’t tell which one to import. I am getting the following error:
C:\laragon\www\elixir\kamaro_ops (Adopting-AshFramework -> origin)
λ iex -S mix phx.server
Compiling 2 files (.ex)
error: undefined function references/1 (there is no such import)
│
27 │ references do
│ ^
│
└─ lib/kamaro/stock/item_category.ex:27:3: Kamaro.Stock.ItemCategory (module)
== Compilation error in file lib/kamaro/stock/item_category.ex ==
** (CompileError) lib/kamaro/stock/item_category.ex: cannot compile module Kamaro.Stock.ItemCategory (errors have been logged)
Here is the join resource: ItemCategory
defmodule Kamaro.Stock.ItemCategory do
use Ash.Resource,
domain: Kamaro.Stock,
data_layer: AshPostgres.DataLayer
postgres do
table "stock_item_categories"
repo Kamaro.Repo
end
actions do
defaults [:create, :read, :destroy]
end
relationships do
belongs_to :item, Kamaro.Stock.Item do
primary_key? true
allow_nil? false
end
belongs_to :category, Kamaro.Stock.Category do
primary_key? true
allow_nil? false
end
end
references do
reference :item, on_delete: :delete, on_update: :update, name: "stock_item_categories_item_id_fkey"
end
end
It is added in the postgre block and it does require generating migrations. I’d suggest familiarizing yourself with the structure of the DSL docs, as the DSL docs do show which section you’re in.
Thank you very much. I will follow your advise.
1 Like