Ash FrameworkAF
Ash Framework3y ago
10 replies
Blibs

Ash.Query.filter creates filter with OR condition instead of AND

Ash.Query.filter function documentation explicitly says that a filter will be added with the AND condition, but it seems that this is not the case when fragments is used.

For example, if I do this:

import Ash.Query

FeedbackCupcake.Feedbacks.Template
|> Ash.Query.new()
|> Ash.Query.for_read(:read, %{})
|> Ash.Query.filter(expr(name == "a"))
|> Ash.Query.filter(expr(fragment("? %> ?", "a", "a")))


This will correctly generate the following query:

#Ash.Query<
  resource: FeedbackCupcake.Feedbacks.Template,
  filter: #Ash.Filter<name == "a" and fragment(
    {:raw, ""},
    {:expr, "a"},
    {:raw, " %> "},
    {:expr, "a"},
    {:raw, ""}
  )>
>


But, if I add another fragment to the condition, like this:

import Ash.Query

FeedbackCupcake.Feedbacks.Template
|> Ash.Query.new()
|> Ash.Query.for_read(:read, %{})
|> Ash.Query.filter(expr(name == "a"))
|> Ash.Query.filter(expr(fragment("? %> ?", "a", "a")))
|> Ash.Query.filter(expr(fragment("? %> ?", "b", "b")))


Now it concatenates the last fragment with a OR:

#Ash.Query<
  resource: FeedbackCupcake.Feedbacks.Template,
  filter: #Ash.Filter<(name == "a" and fragment(
    {:raw, ""},
    {:expr, "a"},
    {:raw, " %> "},
    {:expr, "a"},
    {:raw, ""}
  )) or fragment(
    {:raw, ""},
    {:expr, "b"},
    {:raw, " %> "},
    {:expr, "b"},
    {:raw, ""}
  )>
>


From what I tested, I only was able to reproduce this when adding more than one fragment to the query.
Was this page helpful?