many_to_many update problem
I have a
lib/app/shop/resources/product.ex
Here's what I do in the iex:
That all works nicely. But I can not update the products tags or the even the product:
How can I fix this?
productproduct which has many_to_many tagstags. Creating works. Updating doesn't. Here's the resource code:lib/app/shop/resources/product.ex
defmodule App.Shop.Product do
use Ash.Resource, data_layer: Ash.DataLayer.Ets
attributes do
uuid_primary_key :id
attribute :name, :string
attribute :price, :decimal
end
relationships do
many_to_many :tags, App.Shop.Tag do
through App.Shop.ProductTag
source_attribute_on_join_resource :product_id
destination_attribute_on_join_resource :tag_id
end
end
actions do
defaults [:read, :destroy]
create :create do
primary? true
argument :tags, {:array, :map}
change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
end
create :update do
argument :tags, {:array, :map}
change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
end
end
code_interface do
define_for App.Shop
define :create
define :read
define :by_name, get_by: [:name], action: :read
define :update
define :destroy
end
enddefmodule App.Shop.Product do
use Ash.Resource, data_layer: Ash.DataLayer.Ets
attributes do
uuid_primary_key :id
attribute :name, :string
attribute :price, :decimal
end
relationships do
many_to_many :tags, App.Shop.Tag do
through App.Shop.ProductTag
source_attribute_on_join_resource :product_id
destination_attribute_on_join_resource :tag_id
end
end
actions do
defaults [:read, :destroy]
create :create do
primary? true
argument :tags, {:array, :map}
change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
end
create :update do
argument :tags, {:array, :map}
change manage_relationship(:tags, type: :append_and_remove, on_no_match: :create)
end
end
code_interface do
define_for App.Shop
define :create
define :read
define :by_name, get_by: [:name], action: :read
define :update
define :destroy
end
endHere's what I do in the iex:
iex(1)> good_deal_tag = App.Shop.Tag.create!(%{name: "Good deal"})
iex(2)> yellow_tag = App.Shop.Tag.create!(%{name: "Yellow"})
iex(3)> App.Shop.Product.create!(%{
name: "Banana",
tags: [good_deal_tag, yellow_tag]
})
iex(4) banana = App.Shop.Product.by_name!("Banana", load: [:tags])iex(1)> good_deal_tag = App.Shop.Tag.create!(%{name: "Good deal"})
iex(2)> yellow_tag = App.Shop.Tag.create!(%{name: "Yellow"})
iex(3)> App.Shop.Product.create!(%{
name: "Banana",
tags: [good_deal_tag, yellow_tag]
})
iex(4) banana = App.Shop.Product.by_name!("Banana", load: [:tags])That all works nicely. But I can not update the products tags or the even the product:
iex(16)> App.Shop.Product.update!(banana, %{tags: [yellow_tag]})
** (FunctionClauseError) no function clause matching in Keyword.split/2
[...]
iex(16)> App.Shop.Product.update!(banana, %{name: "test"})
** (FunctionClauseError) no function clause matching in Keyword.split/2iex(16)> App.Shop.Product.update!(banana, %{tags: [yellow_tag]})
** (FunctionClauseError) no function clause matching in Keyword.split/2
[...]
iex(16)> App.Shop.Product.update!(banana, %{name: "test"})
** (FunctionClauseError) no function clause matching in Keyword.split/2How can I fix this?
