astjohn
Hi All,
I’m trying to perform the following SQL query:
SELECT t.*, d.*, u.* FROM
(
SELECT x.* FROM trips AS x
INNER JOIN trips_companies AS tc ON tc.trip_id = x.id
INNER JOIN companies AS c ON tc.company_id = c.id
WHERE c.id = '91955970-34eb-4a1b-97e6-53e9084abbfa'
ORDER BY x.start DESC
LIMIT 100
OFFSET 0
) AS t
INNER JOIN destinations AS d ON d.trip_id = t.id
LEFT JOIN trips_travellers AS tt ON tt.trip_id = t.id
LEFT JOIN users AS u ON tt.user_id = u.id
ORDER BY t.start DESC
My first attempt was the following:
page_number = 1
page_size = 100
offset = page_size * (page_number - 1)
sub = Trip
|> join(:inner, [t], c in assoc(t, :companies))
|> where([_t, c], c.id == ^company.id)
|> order_by([t, _c], desc: t.start)
|> limit(^page_size)
|> offset(^offset)
query = subquery(sub)
|> join(:inner, [t], d in assoc(t, :destinations))
|> join(:left, [t, _d], u in assoc(t, :travellers))
|> order_by([t, _d, _u], desc: t.start)
|> preload([_t, d, u], destinations: d, travellers: u)
I’ve tried a bunch of combinations without any luck. As soon as the preload is introduced, an error is thrown. The only thing I haven’t tried is the use of fragments. Is there a way to do it without fragments?
Thanks!
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
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










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
idi527
What if you try
selectinstead ofpreload?Something like this worked for me once. But a better solution is probably possible.
Ankhers
What is the error that you are getting with the preload?
astjohn
@Ankhers - The error is:
@idi527 - With select instead of preloads, it seemed to work at first. However, the number of results returned was higher than the limit and so likely the joined rows were still included.
We also tried the following:
Which resulted in the number of results being slightly lower that the limit. I’ve yet to find a proper way to do this in one shot via subqueries and joins. Thoughts?
Codball
I’ve been working on something myself. I’ve run into a similar issue where if you have multiple preloaded entries I believe it starts limiting by the preloads as well. Definitely returns an odd form of the data, it’s hard to recognize a pattern. Did you ever come up with a solution?
Here is my module:
and my query: