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