add_tag but unique
A product has a many_to_many relationship to a tag (via a product_tag resource).
lib/app/shop/resources/product.ex This works nicely: Unfortunately I can also add another "Yellow" tag via an update: Obviously this should not happen. The "Yellow" tag should be unique. The longer I think about this the lesser I know how to solve it. Should I through a validation error? How can I do this? Should I just OK it but not adding another tag? How would I do that?
lib/app/shop/resources/product.ex This works nicely: Unfortunately I can also add another "Yellow" tag via an update: Obviously this should not happen. The "Yellow" tag should be unique. The longer I think about this the lesser I know how to solve it. Should I through a validation error? How can I do this? Should I just OK it but not adding another tag? How would I do that?
10 Replies
You can add identities to your tag resource making the names unique, in that case the manage_relationsgip can use the name to lookup existing tags. You might need to add some options to the manage_relationship call, telling it which identities to use.
Can you give me an example of how to setup an identity? https://hexdocs.pm/ash/identities.html doesn't have any examples and the https://ash-hq.org internal search engine doesn't work right now.
Here's my tag resource:
Ash HQ
Ash Framework
A declarative foundation for ambitious Elixir applications. Model your domain, derive the rest.
https://hexdocs.pm/ash/dsl-ash-resource.html#identities unfortunately I only have my phone right now, so it's a bit hard to write code
yep, from that guide:
and then:
Do I use it wrong? Here's the tag resource:
With that I get this error:
I have used identities only with the postgres layer before, looks like you need to configure the pre_check_with option and pass it the API that holds the resource
Yes,
Ash.DataLayer.Ets
doesn't have unique enforcement in the data layer, so it has to do a check before inserting
Ideally we would build that feature into the ETS data layer, but until then it requires that setting to be configuredHow does that work code wise? https://ash-hq.org/docs/dsl/ash-resource#identities-identity-pre_check_with doesn't include an example.
pre_check_with Your.Api
in the identity definition
For the archive:
<1> Use with a PostgreSQL DB.