`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
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
4 Replies
ZachDaniel
ZachDaniel4mo ago
have you looked at the relationships guide? There is a section on manage_relationship. Did an LLM give you that change relation code? That isn't a function https://hexdocs.pm/ash/relationships.html#using-change-manage_relationship-3-in-actions https://hexdocs.pm/ash/Ash.Changeset.html#manage_relationship/4
Vahagn
VahagnOP4mo ago
Yes I have looked into that. I have created a function that returns my change module, which only calls manage_relationship with the same values as I passed them I just want to know if I pass on_match: :error to Changeset.manage_relationship, shouldn't it error out if it finds a record matching the identity?
ZachDaniel
ZachDaniel4mo ago
It should yes. Did you try it? use_identities: [:identity_name]
Vahagn
VahagnOP4mo ago
oh. I thought it is an array that accepts attributes will try thanks

Did you find this page helpful?