Ash FrameworkAF
Ash Framework3y ago
11 replies
Alan Heywood

Multiple filters giving unexpected result

Say I have a data model similar to this:

defmodule Ht.Org do
  has_many :accounts, Ht.Account   
end

defmodule Ht.User do
  has_many :accounts, Ht.Account   
end

defmodule Ht.Account do
  belongs_to :org, Ht.Org
  belongs_to :user, Ht.User
end


I create some records and perform this query (note that create! is my own simple factory helper)

org = create!(:org)

user_1 = create!(:user)
user_1_account = create!(:account, user_id: user_1.id, org_id: org.id)

user_2 = create!(:user)
user_2_account = create!(:account, user_id: user_2.id, org_id: org.id)

Ht.User
|> Ash.Query.filter(id == ^user_1.id)
|> Ash.Query.filter(id == ^user_2.id or accounts.org.id == ^org.id)
|> Api.read(authorize?: false)
|> IO.inspect()


This is returning both user records. My understanding is that multiple Ash.Query.filter calls are combined with and, so the first filter should constrain the results to 1 record at most. Is there anything I am doing wrong in the way my filters are defined?
Was this page helpful?