I am facing an issue with filtering across relationships involving multitenant and non-multitenant resources. Here’s my setup:
User
Group
: related to User viaUserGroup
Permission
: related toGroup
viaGroupPermission
All resources are multitenant except User
.
Loading related resources is working well but direct filter is not working.
Direct filter uses public
schema instead of tenant schema. For the optimization purpose, I want to query DB directly without having to load all data in memory first.
This Does Not Work
require Ash.Query
User
|> Ash.Query.filter(id == ^user.id)
|> Ash.Query.filter(groups.permissions.name == ^action_type)
|> Ash.Query.filter(groups.permissions.resource == ^resource)
|> Ash.exists?(tenant: "company_1", authorize?: false)
It throws (Postgrex.Error) ERROR 42P01 (undefined_table) relation "public.user_groups" does not exist
because it is not using the tenant user_groups
table.
But This Works
Loading relationship works well.
Ash.read(User, load: [groups: :permissions], tenant: "company_1", authorize?: false)