huntz20

huntz20

Nested preload limit - set to 5 but it's returning 3

Hello there im working on pagination on my query

first lets see my query

query
    |> join(:left, [o], op in OrderProduct, as: :order_product, on: o.id == op.order_id)
    |> join(:left, [o, op], p in Product, as: :product, on: op.product_id == p.id)
    |> preload(
         [order_product: op, product: p],
         [
           :order_type,
           :merchant,
           :customer,
           :payment_method,
           order_product: {
             op,
             product: p
           },
         ]
       )

is valid query and its working, after giving it limit the result mess up

what i mean by mess up is i set the limit 5 but it return 3
i thought it was my query but after hour debugging i realize if i remove nested preload to be this

  query
    |> join(:left, [o], op in OrderProduct, as: :order_product, on: o.id == op.order_id)
    |> join(:left, [o, op], p in Product, as: :product, on: op.product_id == p.id)
    |> preload(
         [order_product: op, product: p],
         [
           :order_type,
           :merchant,
           :customer,
           :payment_method,
#           order_product: {
#             op,
#             product: p
#           },
         ]

it working and find any idea what is going here ??

Most Liked

LostKobrakai

LostKobrakai

Essentially yes, but you don’t need to iterate the data manually. If you don’t join associations in your quieries then preloads in ecto will happen in a separate query already. Even though there can be merit in doing less queries to the db there’s also the downside of data duplication within the resultset. Joining assocs just for preloading is imo nothing to blindly default to.

al2o3cr

al2o3cr

Take a look at the query that this is generating - Ecto.Adapters.SQL.to_sql/3 will give you the actual SQL that gets sent.

The usual culprit in cases like this is that LIMIT applies to the total number of rows returned, but when preloading in a single query there will be one row in the result per OrderProduct; if there are orders with more than one product, fewer than 5 orders will be returned.

LostKobrakai

LostKobrakai

Preload in separate queries instead of joining them inline.

Where Next?

Popular in Questions Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
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
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New

We're in Beta

About us Mission Statement