Ash FrameworkAF
Ash Framework8mo ago
12 replies
Vahagn

`on_match: :error` should error on unique key violation?

Hi, I want to make an invitation accept action, invitation has a relationship member which is a belongs_to from Member, it has user_id, entity_id, which is a unique pair, I want to make accept create a :member relationship, so if a user accepts an invitation that is already in entity, it will throw unique identity violation

This is what I have

    update :accept_for_doctor do
      argument :code, :string, allow_nil?: false

      validate attribute_equals(:role, :doctor)
      validate attribute_equals(:code, arg(:code))
      validate attributes_absent(:member_id)

# this is not so important
      change relation(:accepted_doctor, %{user_id: actor(:id), entity_id: attr(:entity_id)},
               on_lookup: :relate,
               on_no_match: :error,
               on_match: :error,
               on_missing: :ignore
             )

# this should create a member, if it does not exist, or error out
# `relation` function just does manage_relationship with the values provided
      change relation(:member, %{user_id: actor(:id), role: :doctor},
               use_identities: [:user_id, :entity_id],
               on_lookup: :relate,
               on_no_match: :create,
               on_match: :error
             )
    end
Was this page helpful?