Preload Association inside an Embedded Schema from the Parent Schema

Hello I am trying to understand how I can preload the association of an embedded schema.

What is the syntax exactly and if there is something I am doing wrong on schema level.

Parent Schema

  schema "media" do
    field(:tags, {:array, :string})
    field(:title, :string)
    field(:author, :string)
    embeds_many :files, File
    field(:type, :string)
    field(:locked_status, :string, default: "locked")
    field(:private_status, :string, dedfault: "private")

    many_to_many(
      Helpers.env(:content_table) |> String.to_atom(),
      Helpers.env(:content_schema),
      join_through: "medias_contents",
      join_keys: [media_id: :id, content_id: :id]
    )

    timestamps()
  end

Embedded Schema

This is the file schema which is the embedded schema that is not supported by a table only exists on ecto level:

  schema "file" do
    field(:url, :string)
    field(:filename, :string)
    field(:type, :string)
    field(:size, :integer)
    field(:duration, :integer)
    belongs_to :platform, Platform, on_replace: :delete

    timestamps()
  end

Embedded schema Association

and this is the platform schema that is associated to the file schema and it is backed with a table on db level:

  schema "platforms" do
    field :description, :string
    field :height, :integer
    field :name, :string
    field :width, :integer
    has_one :file, File
    timestamps()
  end

Problem encountered

I am trying to preload the platform when querying the media however I am having the following error when trying to do so:

iex(14)>   Media.PostgreSQL.Schema |> Repo.get(11) |> Repo.preload(files: :platform)
** (ArgumentError) schema Media.PostgreSQL.Schema does not have association :files
    (elixir 1.10.2) lib/enum.ex:2111: Enum."-reduce/3-lists^foldl/2-0-"/3
[debug] QUERY OK source="media" db=179.2ms idle=1633.8ms
SELECT m0."id", m0."tags", m0."title", m0."author", m0."files", m0."type", m0."locked_status", m0."private_status", m0."inserted_at", m0."updated_at" FROM "media" AS m0 WHERE (m0."id" = $1) [11]

I’m not sure if this will help at all, but I did notice two things here:

schema "file" do

seems like it would be incongruous with

embeds_many :files, File

due to the schema being singular and the association plural.

Similarly,

belongs_to :platform, Platform, on_replace: :delete

and

schema "platforms" do

@fireside68, Thank you for your reply.

However the files embedded schema does not have a name. I updated it to:

  embedded_schema do
    field(:url, :string)
    field(:filename, :string)
    field(:type, :string)
    field(:size, :integer)
    field(:duration, :integer)
    belongs_to :platforms, Platform, on_replace: :delete
  end

Considering your suggestion for the platforms naming I also followed but had the same result:

** (ArgumentError) schema Media.PostgreSQL.Schema does not have association :files

I also referred to this issue also to this PR.

So I concluded the following:

You can preload directly from the embedded schema it self, in my case the file schema. However I can not
preload what’s inside the embedded schema when querying the parent schema in my case the media schema that holds the embedded schema files.

The error stating there is no association is quite correct as media does not have an association with files. However files does have an association with platforms but cannot be preloaded when querying the medias.

I am not sure if this feature is covered or not to preload nested association within embeds directly.