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]