steven7

steven7

Hi I am trying to preload associations for a subquery like such:

from(p in Post, distinct: [p.group_id], order_by: [asc: p.published_date], preload: [:comments])
|> subquery()
|> limit(some_limit)
|> order_by([q], asc: q.title)

but Ecto is throwing a ** (Ecto.QueryError) cannot preload associations in subquery in query.

Is there another way to get around this error? My use case specifically require the use of subquery because I am using Postgres DISTINCT ON clause and Postgres dictates that the ORDER BY must then follow the columns specified in the DISTINCT ON clause.

To shed more light on the use case, imagine I have a table (Post) with a column called group_id (and many Posts will share the same group_id), and my query will returns only 1 Post per group_id where the Post with the earliest published date among the group will be returned. This is easily achievable by distinct: [p.group_id], order_by: [asc: p.published_date] but for the full result sets I actually want them sorted by Post’s title, hence the use of subquery but using subquery somehow eliminate the possibility of preloads. I know there are other ways (that are more SQL-oriented as DISTINCT ON is a Postgres feature like window function) but I find DISTINCT ON the simplest to reason with.

Hence my question is, is there a way to still use preload for subquery?

Showing Posts 1 to 10

dimitarvp

dimitarvp

Let’s not get fixated on how you figure this must be solved for now.

Can you give an example of what must the result set look like exactly?

mayel

mayel

Hi @steven7 I’ve come across the exact same challenge and wondering what solution or workaround you ended up finding?

tfwright

tfwright

Did you try putting the preload on the outer query instead of the sub query?

mayel

mayel

I will try that, thanks

AndrewKlymchuk

AndrewKlymchuk

Hello guys. Is it possible to use named bindings from inner query when I need to preload in parent query?
For me it fail with error like: ** (Ecto.QueryError) unknown bind name ':foo' in query:

jmnda

jmnda

Can you give an example of query you are running, to get more context?

AndrewKlymchuk

AndrewKlymchuk

@jmnda Something like that:

Post
|> join(:inner, [p], a in assoc(p, :author), as: :author)
|> where([author: author], author.reputation > 100)
|> select([p], %{p | rn: row_number() |> over(order_by: p.id)})
|> subquery()
|> select([p], %{p | comments_count_total: sum(p.comments_count) |> over(order_by: a.rn)})
|> where([p], p.rn > 10)
|> subquery()
|> where([p], p.comments_count_total < 1000)
|> preload([author: author], author: author)

It tells me : ** (Ecto.QueryError) unknown bind name ':author' in query:
I don’t want to join authors again, can I reuse them somehow from subquery?

LostKobrakai

LostKobrakai

Yeah because of |> subquery(). The query passed into preload has no :author binding. When using subquery all the returned columns become part of the binding you use subquery for.

zachallaun

zachallaun

Subqueries can refer to a named parent using parent_as: Ecto.Query.API — Ecto v3.14.0

Example from docs:

child_query = from c in Comment, where: parent_as(:posts).id == c.post_id
from p in Post, as: :posts, inner_lateral_join: c in subquery(child_query)
AndrewKlymchuk

AndrewKlymchuk

I don’t think that parent_as is suitable for what I want to achieve because it is used when we need to access binding from parent query in subquery, but I need to do vice versa - access binding in the parent query. Or do I miss something?

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews