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 18 to 9

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

One more typo, try again.

shahryarjb

shahryarjb

I have this error,

** (Postgrex.Error) ERROR 42703 (undefined_column) column u0.subscribers does not exist

    query: SELECT u0."id", u0."name", u0."mobile", u0."last_name", u0."mobile", u0."status", u0."role", u0."inserted_at", u0."updated_at", u0."subscribers", s1."id", s1."expire_time", s1."status", s1."user_id", s1."inserted_at", s1."updated_at" FROM "users" AS u0 LEFT OUTER JOIN "subscribers" AS s1 ON s1."user_id" = u0."id" WHERE (u0."id" = $1)
    (ecto_sql) lib/ecto/adapters/sql.ex:605: Ecto.Adapters.SQL.raise_sql_call_error/1
    (ecto_sql) lib/ecto/adapters/sql.ex:538: Ecto.Adapters.SQL.execute/5
    (ecto) lib/ecto/repo/queryable.ex:147: Ecto.Repo.Queryable.execute/4
    (ecto) lib/ecto/repo/queryable.ex:18: Ecto.Repo.Queryable.all/3
    (ecto) lib/ecto/repo/queryable.ex:66: Ecto.Repo.Queryable.one/3

it should be noted I fixed my problem with long way but if it works I think my code will be Shorter

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

oops, I had a typo, fixed!

shahryarjb

shahryarjb

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

from u0 in BankError.ClientApi.Auth.UserSchema,
  left_join: s1 in BankError.ClientApi.Factor.SubscriberSchema,
  on: s1.user_id == u0.id,
  where: u0.id == ^...,
  select: struct(s1, [:id, :name, :mobile, :last_name, :mobile, :status, :role, :inserted_at, :updated_at, :subscribers]),
  preload: [subscribers: s1]

    (ecto) lib/ecto/repo/queryable.ex:132: Ecto.Repo.Queryable.execute/4
    (ecto) lib/ecto/repo/queryable.ex:18: Ecto.Repo.Queryable.all/3
    (ecto) lib/ecto/repo/queryable.ex:66: Ecto.Repo.Queryable.one/3
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],
       select: struct(u, [
         :id,
         :name,
         :mobile,
         :last_name,
         :mobile,
         :status,
         :role,
         :inserted_at,
         :updated_at,
       ])
shahryarjb

shahryarjb

I mean the user hash password and shows the user status, I want to show my user the query selected.
if I call the u: u in my select this problem will be solved like this

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

   Repo.one(query)

but it shows me Users all fields again

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

When you say the user password, do you mean the user password hash or the user’s actual password? The user’s actual password should never, EVER be stored in the database.

shahryarjb

shahryarjb

if I use this, it shows me user password I need to select fields I want not all of them

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

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
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
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews