alaister

alaister

Hi,
I have the following ecto query:
post_query = from p in Post, where: p.id == ^id, join: c in assoc(p, :comments), join: u in assoc(c, :user), preload: [comments: {c, :user}]

How do I select the following fields: p.title, p.content, p.comments, c.comment, c.inserted_at and u.name?
I’m having trouble with this because of the wired joins I’m doing (Still learning). Also this query actually runs two queries on the database. Is there any way to do it all in one?

Thanks,
Alaister

Showing Posts 1 to 10

Molly101

Molly101

You can use a pattern matching here to get only those fields You’re interested in, i.e. %{title: title, content:content, comments: comments, user: %{name: name}} = Repo.get!(post_query) and then You can pass comments though Enum.Map. In Phoenix app You can also use View - “show.json” approach to convert result of Your ECTO query to a Map with the fields that You need… Because anyway You can’t just encode ECTO query result to JSON…

Linuus

Linuus

This should make only one DB query.

post_query = 
  from p in Post,
    where: p.id == ^id,
    join: c in assoc(p, :comments),
    join: u in assoc(c, :user), 
    preload: [comments: {c, user: u}]

Regarding selecting association fields, you can do something like this (not tested)

post_query = 
  from p in Post,
    where: p.id == ^id,
    join: c in assoc(p, :comments),
    join: u in assoc(c, :user), 
    select: %{id: p.id, title: p.title, comments: p.comments, name: u.name}

Not sure exactly what you want to get back but that’s one way to select fields :slight_smile:

alaister

alaister OP

Thank you @Linuus! I was missing the user: u on the end there. Still not entirely sure what that is doing?

Also the data structure I’m trying to achieve looks like the following:

%{
  content: "This is some content",
  id: "46b7e047-a1f1-4fe1-b7f1-91c2aeb5910d",
  inserted_at: #Ecto.DateTime<2016-06-29 11:49:53>,
  title: "This is a new post"
  comments: [%{
    comment: "Some comment!",
    id: "a94fc738-69e0-44e1-9e39-3204ad7f3878",
    inserted_at: #Ecto.DateTime<2016-07-01 22:48:41>,
    user: %{
      name: "Alaister Young",
    },
    %{
      comment: "Some comment!",
      id: "a94fc738-69e0-44e1-9e39-3204ad7f3879",
      inserted_at: #Ecto.DateTime<2016-07-01 22:48:42>,
      user: %{
        name: "Alaister Young",
      }
    }
  ]
}

The problem I’m having with this structure is I’m not sure how to deal with the list of comments with the nested users.

Thanks again!

Linuus

Linuus

The user: u tells ecto to use that join thingy for the preload and not issue another request to fetch that association. (I think… I’m quite new to Ecto myself.)

So this query:

post_query = 
  from p in Post,
    where: p.id == ^id,
    join: c in assoc(p, :comments),
    join: u in assoc(c, :user), 
    preload: [comments: {c, user: u}]

returns that structure, right? So what you want to do now is limit the number of selected fields for performance reasons?

alaister

alaister OP

That is completely correct!

Linuus

Linuus

I had a discussion about this on IRC and it seems to often be better to not join manually and just preload the data (and let it do multiple queries).

This would issue one request to the DB:

post_query = 
  from p in Post,
    where: p.id == ^id,
    join: c in assoc(p, :comments),
    join: u in assoc(c, :user), 
    preload: [comments: {c, user: u}]

But it would return a lot of excessive data because the join would cause the Post data to be sent for each comment (due to how SQL works). Ecto hides this though by throwing away the excessive data.
You can se this if you run something like the query I wrote before:

post_query = 
  from p in Post,
    where: p.id == ^id,
    join: c in assoc(p, :comments),
    select: %{id: p.id, comments: c}

It will return one struct for each Comment so if a Post has two comments:

[%{
  id: 1,
  comments: %{id: 123, text: "foo"}
}, %{
  id: 1, 
  comments: %{id: 124, text: "Next comment"}
}]`

You don’t see this behaviour when running without the select statement because Ecto takes care of mapping the data.

When just preloading:

post_query = 
  from p in Post,
    where: p.id == ^id,
    preload: [comments: [:user]]

This would issue three requests but no excessive data so in many cases it is actually more efficient (Ecto even does the requests in parallell when possible).
For example, if the same user has made all the comments, the user would only be fetched once. When joining, the user would be returned for each comment.

So, unless you know you have a bottleneck here it may not be worth it…

(I’m quite new to Ecto, so if someone sees something wrong here, please let me know! :slight_smile: )

alaister

alaister OP

Ahh okay that makes a lot of sense! Thanks again :slight_smile:

My only question now is a much more general Ecto question and that’s how to order the comments in descending order. I understand that it has something to do with order_by: [desc: c.inserted_at] but I’m not sure where to put this!

Thanks,
Alaister

alaister

alaister OP

I think I have solved this one:

comment_query = from c in Comment,
                order_by: [desc: c.inserted_at],
                preload: :user

post_query = from p in Post,
             where: p.id == ^id,
             preload: [comments: ^comment_query]

post = Repo.one(post_query)

Also would selecting only the fields I needed make my queries more performant? If so how might I do it with the preloaded users without the join?

Thanks,
Alaister

shahryarjb

shahryarjb

Hello, I have a problem when I want to use select in preload, please see this code

query = from u in UserSchema,
       where: u.id == ^id,
       left_join: c in assoc(u, :subscribers),
       preload: [subscribers: c],
       select: %{
         id: u.id,
         name: u.name,
         mobile: u.mobile,
         last_name: u.last_name,
         mobile: u.mobile,
         status: u.status,
         role: u.role,
         inserted_at: u.inserted_at,
         updated_at: u.updated_at,
         subscribers: c
       }
   Repo.one(query)
  end

and error:

** (Ecto.QueryError) the binding used in `from` must be selected in `select` when using `preload` in query:

I should use select on UserSchema, how can I fix this ?

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe
query = from u in UserSchema,
       where: u.id == ^id,
       left_join: c in assoc(u, :subscribers),
       preload: [subscribers: c]

   Repo.one(query)

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
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
samoloth
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

Other Trending Topics Top

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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews