On Fri, Aug 12, 2022 at 11:15 AM Matt Culpepper <[email protected]> wrote:
> Hey Jeremy,
>
> I remember a while back, I asked about the use of graph and eager_graph
> together, and you said it wasn't possible and (IIRC) to use join instead of
> graph.
>
> I noticed something I thought was strange today.
>
> Whenever I have a dataset with a join and call eager_graph.all, it issues
> 2 queries. So,
>
> Model.where(status: 'pending').eager_graph(:association).all
> => one query, as expected
>
> Model.join(:accounts, id: :account_id).eager_graph(:association).all
>
> => SELECT * FROM "models" INNER JOIN "accounts" ON ("accounts"."id" =
> "models"."account_id")
>
> => SELECT <fields> FROM (
> SELECT * FROM "models" INNER JOIN "accounts" ON ("accounts"."id" =
> "models"."account_id")
> ) AS "models"
> LEFT OUTER JOIN "associations" AS \"associations\" ON <snip>
>
> My use case is that I have a base dataset with joins, but I want to be
> able to support dynamic `?include=association` from query params.
>
> Implemented like this: base_dataset.eager_graph(*includes) if includes.any?
>
> Any ideas on this use case and the extra query?
>
The extra query could be to get columns. However, there should be a LIMIT
0/1 or equivalent in that case.
Do you have to do the join first? Could you do it after the eager_graph?
That may avoid the query. You'll have to use add_graph_aliases if you want
to include any columns from the table you are joining manually, in that
case.
One issue you could have with the join before eager_graph is if you have
any columns with the same name in both models and accounts.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sequel-talk/CADGZSScFaCeou38Do2C5Tbt9Os_ozdcteP2Y5khXOZTjd14Fdw%40mail.gmail.com.