stratigos
Hello! I need to update a dynamically built query such that items with photos order before items without photos. A Photo belongs to an Item.
What I would do in SQL is left join on the association table (“photos”), select a named count via COUNT(photos.id) AS photos_count, and then either order by it directly, or order by a case statement where photos_count > 0 (or IS NOT NULL …).
I am having a very difficult time expressing this with Ecto. I need to define this in a small helper function that is piped into an existing list of query building functions, so I dont have much control over how tables are aliased. Furthermore, all of my efforts to define a named COUNT result in an error like:
undefined function escape_count/0
Ive tried via: query |> select_merge([item, ..., photo], %{photos_count: count(photo.id)}
Or: `query |> select_merge([item, …, photo], fragment(“COUNT(?) AS photos_count”, photo.id))
Or (guessing here) select_merge([item, ..., photo], %{has_photos: fragment("1 CASE (?) IS NULL THEN 0 ELSE 1", photo.id)})
I couldnt find a way of doing this directly in the order_by clause, though that feels unintuitive anyway.
I would just do this in raw SQL, but being part of a dynamically built query, I cant control the table names and such. I also cant do an in-memory sort with elixir code, since other parts of the query control pagination, etc.
How can I append to an existing query, some clauses that will allow me to order by the presence of associations?
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Marked As Solved- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
peerreynders
In the simplest case it is possible as Ecto queries are composable. Example:
In your case, the manner in which you are composing the query may be getting in the way at this point. So you may have to restructure your query composition flow to somehow make the
GROUP BYfit when it is needed.Also Liked
peerreynders
Not sure if this will help in your particular situation but starting simply with this raw SQL:
which can be expressed in keywords form as
or in expression form as
Whether or not you can successfully compose such a query in your particular situation depends entirely on the details …
peerreynders
The standard approach, even in SQL, is to simply repeat the
COUNTexpression in theORDER BYas I have already shown:The RDBMS engine is supposed to correlate that expression to the one in the
SELECTlist without evaluating it twice - so there should be no need to alias the column.Adding the
GROUP BYis the real trick - but it may not be possible to adapt your existing query to tolerating theGROUP BY.Last Post!
stratigos
Yeap, I think you are correct. The problem is not with Ecto lacking any such functionality, but rather a problem any ORM exhibits when dynamically building queries. Looks like the system in which my problem case is exhibited needs redesigned - this is simply the first aggregate query introduced, and thus the first time its showing its limitation.
Refactoring seems to be the answer, plus I may need to upgrade Ecto to get newer features to name/alias JOIN statements (so I can later reference each table in a GROUP BY).