moxley

moxley

Dynamically building an AND expression with Ash.Query

I’m trying to query members that have all the given tag IDs. The code needs to dynamically build an expression like:

member.member_tags.tag_id = tag_id1
AND member.member_tags.tag_id = tag_id2
AND member.member_tags.tag_id = tag_id3

where tag_id1, tag_id2, and tag_id3 come in as a list.

I tried this:

Enum.reduce(tag_ids, query, fn id, query ->
  Ash.Query.filter(query, exists(member_tags.tag_id == ^id))
end)

That results in:

[warning] `1b6d55b5-7d6a-478d-9872-86153c141ff0`: AshGraphql.Error not implemented for error:

** (Ash.Error.Query.NoSuchFunction) No such function exists for resource GF.Members.Member2
    (elixir 1.16.1) lib/process.ex:860: Process.info/2
    (ash 3.1.3) lib/ash/error/query/no_such_function.ex:5: Ash.Error.Query.NoSuchFunction.exception/1
    (ash 3.1.3) lib/ash/filter/filter.ex:3311: Ash.Filter.resolve_call/2
    (ash 3.1.3) lib/ash/filter/filter.ex:2513: Ash.Filter.add_expression_part/3
    (ash 3.1.3) lib/ash/filter/filter.ex:2451: anonymous fn/3 in Ash.Filter.parse_expression/2

I tried playing with Ash.Expr.expr():

expression = Ash.Expr.expr(true)

Enum.reduce(tag_ids, expression, fn id, expression ->
  Ash.Expr.expr(^expression and member_tags.tag_id == ^id)
end)

query = Ash.Query.filter(query, ^expression)

But the filter part of the query always came back as true.

Marked As Solved Switch mode

zachdaniel

zachdaniel

Creator of Ash

The query: [filter: ....] variation won’t work. That is the syntax for inline aggregates, whereas exists is special. The snippet I posed should work.

exists(member_tags, tag_id == ^id)

The kind of query you are writing is, in general, not a very easy query to write. There are a few ways you could do it. Your first example was very close to correct

Using exists

# where a member_tag exists for each tag_id
Enum.reduce(tag_ids, query, fn id, query ->
  Ash.Query.filter(query, exists(member_tags, tag_id == ^id))
end)

Would do what you want. This will result in a rather large SQL query w/ an EXISTS per tag_id you’re searching for (and would probably be fine in general).

Using count/2

However, you could also do something like this which would result in joining once. Only works if your join resource is unique on member_id and tag_id.

# where the number of tags that exist with these tag_ids 
# equals the number of tag_ids I asked for
Ash.Query.filter(
  count(member_tags, query: [filter: tag_id in ^tag_ids]) == ^Enum.count(tag_ids)
)

And here you can see the syntax @sevenseacat was getting at in her first response. That syntax just happens to not apply to exists/2

You may want to profile/try each out to see how it performs in your use case.

Also Liked

moxley

moxley

They both worked!

I had to modify the second one, to add the query first argument

# where the number of tags that exist with these tag_ids 
# equals the number of tag_ids I asked for
Ash.Query.filter(
  query,
  count(member_tags, query: [filter: tag_id in ^tag_ids]) == ^Enum.count(tag_ids)
)

Indeed, the join table should be unique on Member and Tag, so this solution is probably better.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

beepbeepbopbop
SuperAPI will be hosting the Melbourne Elixir meetup this Thursday, August 6th. The event will be held at Level 2, 525 Collins St (Rialto...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement