AF
Ash Framework•5mo ago
Vahagn

manage_relationship does not insert

create :register do
accept [:name]
argument :members, {:array, :map}, public?: false, default: []

change manage_relationship(:members, type: :create, on_match: :ignore, on_no_match: :create) do
[%{
user_id: actor(:id),
role: "owner",
entity_id: ref(:id)
}]
end

load :members
end
create :register do
accept [:name]
argument :members, {:array, :map}, public?: false, default: []

change manage_relationship(:members, type: :create, on_match: :ignore, on_no_match: :create) do
[%{
user_id: actor(:id),
role: "owner",
entity_id: ref(:id)
}]
end

load :members
end
I just tried to create a :register action for Entity to create Member with actor as owner, but it only inserts in entities table
14 Replies
ZachDaniel
ZachDaniel•5mo ago
That syntax is incorrect Not sure where the do block w/ input came from (I suspect an LLM?) When you want to provide input like that, (i.e not map an argument value directly to a manage_relationship) then you have to do it in a custom change
create :register do
accept [:name]
change fn changeset, context ->
Ash.Changeset.manage_relationship(changeset, :members, [...input], type: :create)
end

load :members
end
create :register do
accept [:name]
change fn changeset, context ->
Ash.Changeset.manage_relationship(changeset, :members, [...input], type: :create)
end

load :members
end
Please see the #rules
AI generated code must also be marked as such, especially when requesting support or help related to it.
(no worries, its a new rule)
Vahagn
VahagnOP•5mo ago
Sorry about that. It worked, still cannot get the actor in there :))))
ZachDaniel
ZachDaniel•5mo ago
context.actor
Vahagn
VahagnOP•5mo ago
Somehow passing for_create(:register, actor: user) does not work in tests And when I add the relationship to Entity, the entity_id does not get added, and it is not present in the changeset, Should I manually create it in after_change? Or is there a way to specify that after creating Entity, the id should be put in relationships
ZachDaniel
ZachDaniel•5mo ago
You don't need to add the entity_id manage_relationship handles that It handles the source_attribute at least, if that is what entity_id is
Vahagn
VahagnOP•5mo ago
actions do
defaults [:read, :destroy, update: :*]

create :register do
accept [:name]

argument :members, {:array, :map}, default: []

change fn changeset, context ->
IO.inspect(context, label: "Context in change")

changeset =
Ash.Changeset.manage_relationship(
changeset,
:members,
%{user_id: context.actor.id, role: "owner"},
type: :create
)

IO.inspect(changeset, label: "BAfter manage relationship")
end
end
end
actions do
defaults [:read, :destroy, update: :*]

create :register do
accept [:name]

argument :members, {:array, :map}, default: []

change fn changeset, context ->
IO.inspect(context, label: "Context in change")

changeset =
Ash.Changeset.manage_relationship(
changeset,
:members,
%{user_id: context.actor.id, role: "owner"},
type: :create
)

IO.inspect(changeset, label: "BAfter manage relationship")
end
end
end
ZachDaniel
ZachDaniel•5mo ago
That looks right to me
Vahagn
VahagnOP•5mo ago
I have made something like this, can't get LLM to write me a simple manage_relationshiop
Vahagn
VahagnOP•5mo ago
No description
ZachDaniel
ZachDaniel•5mo ago
What does the relationship look like? is entity_id the source_attribute?
Vahagn
VahagnOP•5mo ago
relationships do
belongs_to :user, Aldente.Accounts.User
belongs_to :entity, Aldente.Management.Entity
end
relationships do
belongs_to :user, Aldente.Accounts.User
belongs_to :entity, Aldente.Management.Entity
end
ZachDaniel
ZachDaniel•5mo ago
What does the primary create action look like on member?
Vahagn
VahagnOP•5mo ago
create :create do
primary? true
accept [:role, :user_id, :entity_id]
argument :role, :string, allow_nil?: false
argument :user_id, :uuid, allow_nil?: false
argument :entity_id, :uuid, allow_nil?: false
end
create :create do
primary? true
accept [:role, :user_id, :entity_id]
argument :role, :string, allow_nil?: false
argument :user_id, :uuid, allow_nil?: false
argument :entity_id, :uuid, allow_nil?: false
end
This is also LLM code adding all the arguments Isn't accept enough? Just removed argument rows and it worked fine
ZachDaniel
ZachDaniel•5mo ago
Yeah those arguments shouldn't be there 😄 Just the accept

Did you find this page helpful?