Problem in using manage_relationship and Ash events with tenant

Hi, this is my update action, that i use for normal user which has tenant and site_id
update :update do
primary? true
require_atomic? false
transaction? true
accept @admin_fields |> List.delete(:site_id)

argument :tag_ids, {:array, :uuid} do
description "List of tag IDs to assign to this section"
end

change slugify(:slug, into: :slug, ignore: [".", "!", "?"])

change manage_relationship(:tag_ids, :tags,
type: :append_and_remove,
on_no_match: :error,
on_match: :ignore
)

# Add metadata for event tracking
change fn changeset, context ->
Ash.Changeset.put_context(changeset, :ash_events_metadata, %{
site_id: context.tenant || changeset.data.site_id,
title: Ash.Changeset.get_attribute(changeset, :title) || changeset.data.title
})
end

description "Updates section for tenant users - requires tenant context, prevents site_id changes"
end
update :update do
primary? true
require_atomic? false
transaction? true
accept @admin_fields |> List.delete(:site_id)

argument :tag_ids, {:array, :uuid} do
description "List of tag IDs to assign to this section"
end

change slugify(:slug, into: :slug, ignore: [".", "!", "?"])

change manage_relationship(:tag_ids, :tags,
type: :append_and_remove,
on_no_match: :error,
on_match: :ignore
)

# Add metadata for event tracking
change fn changeset, context ->
Ash.Changeset.put_context(changeset, :ash_events_metadata, %{
site_id: context.tenant || changeset.data.site_id,
title: Ash.Changeset.get_attribute(changeset, :title) || changeset.data.title
})
end

description "Updates section for tenant users - requires tenant context, prevents site_id changes"
end
so when i want to do for the user which is master (and no site_id) i do like this
action :master_update, :struct do
description "Updates section for master users - requires nil tenant context, prevents site_id changes"
constraints instance_of: MishkaDocument.Section

argument :id, :uuid, allow_nil?: false
arguments ......
argument :tag_ids, {:array, :uuid}

run fn input, _context ->
id = input.arguments.id

case Ash.get(MishkaDocument.Section, id, action: :master_get, authorize?: false) do
{:ok, record} when not is_nil(record) ->
tenant = record.site_id

record
|> Ash.Changeset.for_update(:update, input.arguments, tenant: tenant, authorize?: false)
|> Ash.update()

_ ->
{:error, Ash.Error.Query.NotFound.exception(resource: MishkaDocument.Section)}
end
end
end
action :master_update, :struct do
description "Updates section for master users - requires nil tenant context, prevents site_id changes"
constraints instance_of: MishkaDocument.Section

argument :id, :uuid, allow_nil?: false
arguments ......
argument :tag_ids, {:array, :uuid}

run fn input, _context ->
id = input.arguments.id

case Ash.get(MishkaDocument.Section, id, action: :master_get, authorize?: false) do
{:ok, record} when not is_nil(record) ->
tenant = record.site_id

record
|> Ash.Changeset.for_update(:update, input.arguments, tenant: tenant, authorize?: false)
|> Ash.update()

_ ->
{:error, Ash.Error.Query.NotFound.exception(resource: MishkaDocument.Section)}
end
end
end
as you see i call the normal update action with tenant and it works. but when i use the ash events and add update action inside the only_actions it returns this warning πŸ‘‡πŸ‘‡πŸ‘‡
Solution:
Please do!
Jump to solution
2 Replies
Shahryar
ShahryarOPβ€’4w ago
Warning
01:24:34.100 request_id=GHOyEwn-EFetC_MAAGAB [warning] `ca30d1de-2807-46f7-b0dd-0d325b66d964`: AshJsonApi.Error not implemented for error:

** (Ash.Error.Invalid.TenantRequired) Bread Crumbs:
> Error returned from: MishkaDocument.Tag.public_list
> Error returned from: MishkaDocument.Section.read
> Error returned from: MishkaDocument.Section.update


Queries against the MishkaDocument.Tag resource require a tenant to be specified
01:24:34.100 request_id=GHOyEwn-EFetC_MAAGAB [warning] `ca30d1de-2807-46f7-b0dd-0d325b66d964`: AshJsonApi.Error not implemented for error:

** (Ash.Error.Invalid.TenantRequired) Bread Crumbs:
> Error returned from: MishkaDocument.Tag.public_list
> Error returned from: MishkaDocument.Section.read
> Error returned from: MishkaDocument.Section.update


Queries against the MishkaDocument.Tag resource require a tenant to be specified
when i delete the update from
events do
event_log MishkaDocument.EventLog
only_actions [:create, :master_create, :unarchive, :permanent_destroy]
end
events do
event_log MishkaDocument.EventLog
only_actions [:create, :master_create, :unarchive, :permanent_destroy]
end
It works and no problem!! how can i fix this? By the way if i delete the tag_ids manage_relationship and even the update exists in only_actions it works without problem! And i have set the tenant why still it is finding the tenant when Ash events is set πŸ₯² without it, it consider the tenant but with ash events i think it clear the tenant that i set Full warning: https://gist.github.com/shahryarjb/d990e59d59222a6b649b77faadd313b5 Thank you in advance I even today put some print inside ash_events/lib/events/update_action_wrapper.ex, but it has tenant that i set and has no problem, but i do not know why it can not pass the tenant when Ash event is enabled for update action! for example i have many to many Blog post -> okey Tag -> return tenant nil (without Ash event this knows the tenant we set inside Blog post) Blog Tag -> before Tag can not know about tenant that we set in Blog post so do not anything Hi @Zach , I’m really sorry for tagging you πŸ˜” I know you’re quite busy. I was wondering if you could give me a bit of guidance on where I should look to track down this issue. I’ve tested the entire Ash Events library but haven’t been able to find where the problem might be. It might be something I can help fix if I can locate it. Thank you in advance I found an easy way inside ash source ash/lib/ash/actions/managed_relationships.ex inside load and the relationships we change the tenant = engine_opts[:tenant] to this
tenant = engine_opts[:tenant] || changeset.tenant
tenant = engine_opts[:tenant] || changeset.tenant
and it fixes the problem! i did not send the PR, because other places maybe the creator of this problem! (that i could not be able to find) But i think it is safe place because it is the last place it excitable, and my 4k tests and ash tests passed and it works!
I think cause of this problem because ash update is like manual update not the relation like, so it dose not consider the changeset tenant
If you are good i can send it as PR Thank you @Zach
Solution
ZachDaniel
ZachDanielβ€’4w ago
Please do!

Did you find this page helpful?