colin
Trouble with many to many relationship set by value
I’m struggling trying to setup a many_to_many relationship for Tags. I’d like to create an action that updates the tags by providing the tag name and not the id . I’d like it to use existing tag records if they already exist with that name or create new ones if they don’t already exist. I have the relationship working but it keeps trying to re-create the same tags. Is there a way to get Ash to lookup existing tags using the name value?
Minimal version of my code:
defmodule Catalog.Tag do
...
attributes do
uuid_primary_key :id
attribute :name, :string do
allow_nil? false
end
end
identities do
identity :unique_tag, :name
end
end
defmodule Catalog.ProductTag do
...
attributes do
uuid_primary_key :id
end
identities do
identity :unique_product_tag, [:product_id, :tag_id]
end
relationships do
belongs_to(:product, Shopkeeper.Catalog.Product)
belongs_to(:tag, Shopkeeper.Catalog.Tag)
end
end
defmodule Catalog.Product do
...
actions do
...
update :set_tags do
accept([])
argument(:tags, {:array, :map}, allow_nil?: false)
change(
manage_relationship(
:tags,
on_lookup: :relate,
on_no_match: :create,
on_match: :ignore,
on_missing: :unrelate
)
)
end
end
relationships do
many_to_many(:tags, Catalog.Tag) do
through Catalog.ProductTag
source_attribute_on_join_resource :product_id
destination_attribute_on_join_resource :tag_id
end
end
end
When running these commands I get the following error:
iex(6)> {:ok, product} = Product.set_tags(product, [%{name: "My Tag"}])
iex(7)> {:ok, product} = Product.set_tags(product, [%{name: "My Tag"}])
** (MatchError) no match of right hand side value: {:error, %Ash.Error.Invalid{errors: [%Ash.Error.Changes.InvalidAttribute{field: :product_id, message: "has already been taken", private_vars: [constraint: "product_tags_unique_product_tag_index", constraint_type: :unique], value: nil, changeset: nil, query: nil, error_context: [], vars: [], path: [], stacktrace: #Stacktrace<>, class: :invalid}], stacktraces?: true, changeset: #Ash.Changeset<api: Shopkeeper.Catalog, action_type: :create, action: :create, attributes: %{id: "23d907b5-e94f-4bae-b9bd-8f91a3f86c9a", product_id: "42bc8558-61e9-4c6f-9808-3b6aebe38064", tag_id: "6016b8d8-8198-4e6f-ab88-ce013dbb4f0b"}, relationships: %{}, errors: [%Ash.Error.Changes.InvalidAttribute{field: :product_id, message: "has already been taken", private_vars: [constraint: "product_tags_unique_product_tag_index", constraint_type: :unique], value: nil, changeset: nil, query: nil, error_context: [], vars: [], path: [], stacktrace: #Stacktrace<>, class: :invalid}], data: #Shopkeeper.Catalog.ProductTag<tag: #Ash.NotLoaded<:relationship>, product: #Ash.NotLoaded<:relationship>, __meta__: #Ecto.Schema.Metadata<:built, "product_tags">, id: nil, product_id: nil, tag_id: nil, aggregates: %{}, calculations: %{}, ...>, context: %{actor: nil, authorize?: false, accessing_from: %{name: :tags_join_assoc, source: Shopkeeper.Catalog.Product}}, valid?: false>, query: nil, error_context: [nil], vars: [], path: [], stacktrace: #Stacktrace<>, class: :invalid}}
I’d expect this to be a no-op instead as the tag has already been assigned.
Marked As Solved
barnabasJ
Ash Core Team
You need to specify the :use_identities
option and tell it to use the :unique_tag identity
3
Popular in Questions
I would like to know what is the best IDE for elixir development?
New
If I have a post route which an argument:
post /my_post_route/:my_param1, MyController.my_post_handler
How would get the post params ...
New
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
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
I tried installing
elixir 1.11.2
erlang 23.3.4
via asdf in my zsh shell. Enabled the versions locally and globally.
When I list them ...
New
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
New
Other popular topics
Update:
How to use the Blogs & Podcasts section
You can post links to your blog posts or podcasts either in one of the Official Blog...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Latest Ash Threads
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









