Ash FrameworkAF
Ash Frameworkโ€ข3mo agoโ€ข
3 replies
Shahryar

Testing `filter expr` inside `many_to_many` relation

Based on the help of Zach from https://github.com/ash-project/ash_json_api/pull/392#issuecomment-3417372205
I have a relation like this
    many_to_many :all_posts, MishkaBlog.BlogPost do
      description "All blog posts (bypass tenant) that belong to this collection"
      through MishkaBlog.PostCollection
      source_attribute_on_join_resource :collection_id
      destination_attribute_on_join_resource :post_id
      read_action :read_any # multitenancy :bypass

      # Only allow loading if actor has no tenant (master admin)
      filter expr(is_nil(^actor(:tenant_id)))
    end

For testing i did like this, and it works
collection
|> Ash.load!(:all_posts, authorize?: false, actor: %{tenant_id: nil})


But for testing nil tenant_id, i am doing like this
read :master_get do
    multitenancy :bypass
    get? true
    description "Retrieves single collection by ID, bypassing tenant filtering"
end

Ash.get!(Collection, collection.id, action: :master_get, authorize?: false)
|> Ash.load!(:all_posts,
    authorize?: false,
    actor: %{tenant_id: Ash.UUID.generate()}
)


I get this error, even with Ash.load without !
     ** (Ash.Error.Unknown)
     Bread Crumbs:
       > Exception raised in: MishkaBlog.BlogPost.read_any
       > Exception raised in: MishkaBlog.Collection.read

     Unknown Error

     * ** (Postgrex.Error) ERROR 42P18 (indeterminate_datatype) could not determine data type of parameter $1


My question is, the filter expr(is_nil(^actor(:tenant_id))) is working correctly? and my main problem is how can have it as tupple instead of database level error

Thank you in advance
Solution
is_nil(type(^actor(:id), :uuid))
Was this page helpful?