I am not able to fetch products for a loaded publisher.
migrations:
defmodule Catalog.Repo.Migrations.CreatePublisher do
use Ecto.Migration
def change do
create table(:publishers, primary_key: false) do
add :id, :uuid, primary_key: true, default: fragment("uuid_generate_v4()")
add :name, :string, null: false
end
end
end
defmodule Catalog.Repo.Migrations.CreateVolume do
use Ecto.Migration
def change do
create table(:products, primary_key: false) do
add :id, :uuid, primary_key: true, default: fragment("uuid_generate_v4()")
add :publisher_id, references(:publishers, type: :uuid)
add :name, :string, null: false
end
end
end
models:
defmodule Catalog.Publisher do
use Catalog.Web, :model
@primary_key {:id, :binary_id, autogenerate: false}
schema "publishers" do
field :name, :string
has_many :products, Catalog.Product
end
end
defmodule Catalog.Product do
use Catalog.Web, :model
@primary_key {:id, :binary_id, autogenerate: false}
schema "products" do
field :name, :string
belongs_to :publisher, Catalog.Publisher
end
end
And loading publisher with products:
query = from _ in Catalog.Publisher, limit: 1
publisher = query |> Catalog.Repo.one |> Catalog.Repo.preload(:products)
Unfortunately there is something wrong because I am receiving error:
** (Ecto.Query.CastError) deps/ecto/lib/ecto/association.ex:399: value `"de397199-e3dc-4974-9924-4ce1fa93b1d6"` in `where` cannot be cast to type :id in query:
from v in Catalog.Product,
where: v.publisher_id == ^"de397199-e3dc-4974-9924-4ce1fa93b1d6",
order_by: [asc: v.publisher_id],
select: {v.publisher_id, v}