huntz20

huntz20

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 ??

Showing Posts 1 to 6

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.

huntz20

huntz20 OP

Is there any options for me to make pagination but still doing preload ?

LostKobrakai

LostKobrakai

Preload in separate queries instead of joining them inline.

huntz20

huntz20 OP

sorry i dont realy get want you mean

so basicly i need get the data first then i iterate trrough all data to get all associate data ?

is that best practice for this case ?? or i miss intepret you sugesstion ??

dorgan

dorgan

I’m using this to preload n items per row: Ecto preload n items per entity in a list · GitHub

Would love to know if folks find issues with that approach, but so far it worked well for me

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews