dalerka

dalerka

How to avoid applying the "limit:" to associated schemas?

I’m trying to list some users with links to their posts (per specific topic), and have some basic pagination in place.

Users have many Posts preloaded like so:

query1 = from User, 
    offset: ^((page - 1) * per_page),
    limit: ^per_page    

query2 = from u in query1, 
      distinct: true,
      join: p in assoc(u, :posts),
      on: p.poster_id == u.id,
      where: p.topic_id in subquery(query_topic_ids())
      group_by: [u.id, p.id],
      preload: [posts: p]

Posts also belong to Topics (used here to load only relevant users)

Expected

When supplied a per_page: 1 as parameter the Repo.all(query2) should return 1 User with all associated Posts.

Actual

I get 1 User with only 1 associated post (although there are more posts).

So, how do I restrict the scope of the limit: operator to the parent schema (User) only?
How do I otherwise load and paginate only the users who posted under specific topic and also load such posts per user.

PS: Couldn’t find relevant info in the docs and on this forum or SO, and will appreciate any hints and pointers!

Marked As Solved

sb8244

sb8244

Author of Real-Time Phoenix

If you’re looking to preload the posts for the user, then you’re best off by saying preload: [:posts] instead of by joining with a limit.

The reason for this is that when you do a join in sql, you’re going to get 1 row with the left side and right side together, per match. If you apply a limit, it’s going to apply to the final match joined rows and you’ll get 1 result back.

If your association definition isn’t enough to do the preload, you’ll get extra rows back that you didn’t intend for. You can provide a query to the preload and apply your additional joins criteria. Examples of that are in the preload documentation.

Also Liked

LostKobrakai

LostKobrakai

This is how sql works. Limit is based on the returned rows, not any kind of table. The only way to do what you‘re asking is loading posts in an extra query instead of joining them.

Where Next?

Popular in Questions Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
New
mgjohns61585
Could someone help me? I’m making my first elixir program, number guessing game. I can’t figure out how to convert the user’s guess from ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement