hbbn623

hbbn623

Hello,

I need to preload an association on a get_by function, everything seems good but in the response it tell me there is no association

Here’s the concerned code :

Schema of the association :

  schema "users_cryptos" do
    belongs_to :user, Accounts.User
    belongs_to :crypto, Currencies.Crypto

    timestamps()
  end

  @doc false
  def changeset(user_crypto, attrs) do
    user_crypto
    |> cast(attrs, [:user_id, :crypto_id])
    |> validate_required([:user_id, :crypto_id])
    |> unique_constraint(:unique_subs, name: :subs_index)
  end

Controller calling the function :

  def index_by_user(conn, %{"user_id" => user_id}) do
    preloads = [:cryptos, :users]
    user_cryptos = Accounts.get_user_crypto_from_user!(user_id, preloads)
    render(conn, "index.json", users_cryptos: user_cryptos)
  end

Function in the context :

  def get_user_crypto_from_user!(user_id, preloads) do
    from(uc in UserCrypto, where: uc.user_id == ^user_id)
    |> Repo.all
    |> Repo.preload(preloads)
  end

If anyone sees the problem please take meout of Elixir preloads :')

Showing Posts 1 to 4

al2o3cr

al2o3cr

The names passed to preload should match the names of the associations you want to preload.

In your case, that would mean passing preloads = [:crypto, :user] (no s) to get_user_crypto_from_user!

hbbn623

hbbn623 OP

Thanks, i saw this just after your message…

Now it gives me a less understandable error, only in the console. I’ll take some time to get where’s the problem

[error] Task #PID<0.796.0> started from #PID<0.793.0> terminating
** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for Accounts.User of type Atom, the given module does not exist. This protocol is implemented for the following type(s): Atom, Ecto.Query, Tuple, Ecto.SubQuery, BitString
    (ecto 3.9.2) lib/ecto/queryable.ex:40: Ecto.Queryable.Atom.to_query/1
    (ecto 3.9.2) lib/ecto/query/builder/from.ex:145: Ecto.Query.Builder.From.apply/5
    (ecto 3.9.2) lib/ecto/association.ex:1041: Ecto.Association.BelongsTo.assoc_query/3
    (ecto 3.9.2) lib/ecto/repo/preloader.ex:244: Ecto.Repo.Preloader.fetch_query/8
    (elixir 1.12.2) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
    (elixir 1.12.2) lib/task/supervised.ex:35: Task.Supervised.reply/5
    (stdlib 3.17) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Function: &:erlang.apply/2
    Args: [#Function<8.13453492/1 in Ecto.Repo.Preloader.maybe_pmap/3>, [#Function<20.13453492/1 in Ecto.Repo.Preloader.prepare_queries/6>]]
[error] Task #PID<0.797.0> started from #PID<0.793.0> terminating
** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for Currencies.Crypto of type Atom, the given module does not exist. This protocol is implemented for the following type(s): Atom, Ecto.Query, Tuple, Ecto.SubQuery, BitString
    (ecto 3.9.2) lib/ecto/queryable.ex:40: Ecto.Queryable.Atom.to_query/1
    (ecto 3.9.2) lib/ecto/query/builder/from.ex:145: Ecto.Query.Builder.From.apply/5
    (ecto 3.9.2) lib/ecto/association.ex:1041: Ecto.Association.BelongsTo.assoc_query/3
    (ecto 3.9.2) lib/ecto/repo/preloader.ex:244: Ecto.Repo.Preloader.fetch_query/8
    (elixir 1.12.2) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
    (elixir 1.12.2) lib/task/supervised.ex:35: Task.Supervised.reply/5
    (stdlib 3.17) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Function: &:erlang.apply/2
    Args: [#Function<8.13453492/1 in Ecto.Repo.Preloader.maybe_pmap/3>, [#Function<20.13453492/1 in Ecto.Repo.Preloader.prepare_queries/6>]]
[error] Ranch protocol #PID<0.793.0> of listener CryptoApiWeb.Endpoint.HTTP (connection #PID<0.782.0>, stream id 3) terminated
an exception was raised:
    ** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for Accounts.User of type Atom, the given module does not exist. This protocol is implemented for the following type(s): Atom, Ecto.Query, Tuple, Ecto.SubQuery, BitString
        (ecto 3.9.2) lib/ecto/queryable.ex:40: Ecto.Queryable.Atom.to_query/1
        (ecto 3.9.2) lib/ecto/query/builder/from.ex:145: Ecto.Query.Builder.From.apply/5
        (ecto 3.9.2) lib/ecto/association.ex:1041: Ecto.Association.BelongsTo.assoc_query/3
        (ecto 3.9.2) lib/ecto/repo/preloader.ex:244: Ecto.Repo.Preloader.fetch_query/8
        (elixir 1.12.2) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
        (elixir 1.12.2) lib/task/supervised.ex:35: Task.Supervised.reply/5
        (stdlib 3.17) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
al2o3cr

al2o3cr

Check your user_crypto.ex file - the messages suggest that Accounts.User and Currencies.Crypto aren’t being aliased correctly.

In a typical Phoenix app, they would have full names like SomeApplicationName.Accounts.User and SomeApplicationName.Currencies.Crypto and then user_crypto.ex would include:

alias SomeApplicationName.Accounts
alias SomeApplicationName.Currencies

inside the defmodule block.

hbbn623

hbbn623 OP

It did work indeed, i don’t know how anything worked without alias before that, thanks a lot i can finally generalize that to all my preloads problems :')

— 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
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
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
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
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
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
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
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews