maz
I’m doing a fairly complex join where I want to preload an array of ContentMediaItems (called :contents) that are nested in an association, Channel
But it appears that ecto thinks I’m referencing the MediaItem in the preload even though I’m doing:
preload: [channel: {chan, contents: chancon}],
and name the associations previously:
join: chan in assoc(con, :channel),
join: chancon in assoc(chan, :contents),
So why does the exception
** (Ecto.QueryError) field `MyWord.Multimedia.MediaItem.channel` in preload is not an association in query:
appear?
Here is the full query along with the error below it.
from(m in MediaItem,
join: con in ContentMediaItems,
on: m.id == con.media_item_id,
where: is_nil(m.published_at) == false,
join: chan in assoc(con, :channel),
where: chan.id == con.channel_id,
join: chancon in assoc(chan, :contents),
on: chancon.channel_id == chan.id,
join: org in Org,
on: m.org_id == org.id,
where: org.slug == ^org_slug,
order_by: [{:desc, :published_at}],
preload: [:mediaitemartifacts],
preload: [channel: {chan, contents: chancon}],
select: %{id: m.id, inserted_at: m.inserted_at, media_item: m, channel: chan}
)
Exception:
** (Ecto.QueryError) field `MyWord.Multimedia.MediaItem.channel` in preload is not an association in query:
from m0 in MyWord.Multimedia.MediaItem,
join: c1 in MyWord.Channels.ContentMediaItems,
on: m0.id == c1.media_item_id,
join: c2 in MyWord.Channels.Channel,
on: c2.id == c1.channel_id,
join: c3 in MyWord.Channels.ContentMediaItems,
on: c3.channel_id == c2.id and c3.channel_id == c2.id,
join: o4 in MyWord.Orgs.Org,
on: m0.org_id == o4.id,
where: is_nil(m0.published_at) == false,
where: c2.id == c1.channel_id,
where: o4.slug == ^"myword-app",
order_by: [desc: m0.published_at],
limit: ^41,
select: %{id: m0.id, inserted_at: m0.inserted_at, media_item: m0, channel: c2},
preload: [:mediaitemartifacts],
preload: [channel: {c2, contents: c3}]
Relevant schema information:
schema "mediaitems" do
has_many(:mediaitemartifacts, MyWord.Multimedia.MediaItemArtifact)
many_to_many(:channels, Channel, join_through: "channels_content_media_items", unique: true)
schema "channels" do
has_many(:contents, ContentMediaItems)
schema "channels_content_media_items" do
belongs_to :channel, Channel
belongs_to :media_item, MediaItem
Trending in Questions
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
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
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
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
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
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
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
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
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
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
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
jswanner
When using
assoc/3orpreloadthe name of the association given as an argument needs to match the name given in the schema (including pluralization).In the case of your error message, I’m pretty sure you need to use
:channelsnot:channel.sodapopcan
Chiming in to say: when preloading in the query with complex joins like that, it can often be very slow. This is because only one query is run which returns a massive, de-normalized table that has to filtered down by Elixir. I would try it out with
Repo.preloadas well to see if there is a difference. There might not be, but from experience I once had a response time go from over 30 seconds to under 1 second just by switching toRepo.preload. Fewer queries aren’t always better!maz
This is really good advice and is what I ended up doing.
So to help anyone who’s stuck with a similar problem, here is my less-heavyweight query(I only Ecto.preload
:mediaitemartifactsat first).and later on, in the controller, I Repo.preload() the contents: