maz

maz

Ecto Query preloading complex association

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

Marked As Solved

jswanner

jswanner

When using assoc/3 or preload the 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 :channels not :channel.

Also Liked

sodapopcan

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.preload as 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 to Repo.preload. Fewer queries aren’t always better!

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

We're in Beta

About us Mission Statement