AF
Ash Framework•3mo ago
giusdp

Error tenant required in calculation on tenant resource

Hello! I'm getting an error I don't really know how to solve. It might be a dumb error from my side. I'm trying to count the number of rows of a resource inside a tenant (the 'items' table) and have it as a calculation on my tenant (the 'project' resource). This way I can know from the outside of the multi-tenancy world how many items are inside a project. I've added this simple action on the Item resource:
action :item_count, :integer do
run fn _input, _context ->
Ash.count(__MODULE__)
end
end
action :item_count, :integer do
run fn _input, _context ->
Ash.count(__MODULE__)
end
end
I created a module calculation with this single function (I'm using a code interface for that action):
@impl true
def calculate(records, _opts, _ctx) do
Enum.map(records, fn project ->
MyDomain.count_items(tenant: project authorize?: false)
end)
end
@impl true
def calculate(records, _opts, _ctx) do
Enum.map(records, fn project ->
MyDomain.count_items(tenant: project authorize?: false)
end)
end
No matter what I get a Invalid.TenantRequired when I call count items. The project does implement the ToTenant and I also tried passing the tenant string directly (proj_ <> id). Should I do this stuff in an entirely different way perhaps?
3 Replies
giusdp
giusdpOP•3mo ago
I just noticed now I didn't pass the tenant in the Ash.count(..) function.... -.-
barnabasj
barnabasj•3mo ago
just wanted to say that 😉 does it work? if not you might need to do
action :item_count, :integer do
run fn _input, context ->
Ash.Query.for_read(__MODULE__, %{}, scope: context)
|> Ash.count()
end
end
action :item_count, :integer do
run fn _input, context ->
Ash.Query.for_read(__MODULE__, %{}, scope: context)
|> Ash.count()
end
end
giusdp
giusdpOP•3mo ago
It was driving me crazy 😂 well maybe it might be helpful for others in case they use custom actions this will make them look twice if they are passing stuff around correctly yep it works passing the tenant, I'll try with the scope as well (I've not been using that yet) Awesome, updated into:
run fn _input, context ->
Ash.count(__MODULE__, scope: context)
end
run fn _input, context ->
Ash.count(__MODULE__, scope: context)
end
Thanks a lot!

Did you find this page helpful?