polypush135
(Ecto.Query.CastError) value `"some uuid"` in `where` cannot be cast to type :id in query:
just trying to preload an association
p = Pix |> Repo.get("074a1581-fd36-49e7-a756-03d30f378312")
%PolymorphicProductions.Social.Pix{
__meta__: #Ecto.Schema.Metadata<:loaded, "pics">,
asset: "https://d1sv288qkuffrb.cloudfront.net/polymorphic-productions/photos/37409259750_420cebcc8a_b.jpg",
asset_preview: "https://d1sv288qkuffrb.cloudfront.net/polymorphic-productions/photos/preview/37409259750_420cebcc8a_b.jpg",
comments: #Ecto.Association.NotLoaded<association :comments is not loaded>,
description: "asdf",
id: "074a1581-fd36-49e7-a756-03d30f378312",
inserted_at: ~N[2018-11-04 18:58:03.347422],
photo: nil,
updated_at: ~N[2018-11-04 18:58:03.347444]
}
iex(10)> p |> Repo.preload([:comments])
** (Ecto.Query.CastError) deps/ecto/lib/ecto/association.ex:516: value `"074a1581-fd36-49e7-a756-03d30f378312"` in `where` cannot be cast to type :id in query:
from c in PolymorphicProductions.Social.Comment,
where: c.pix_id == ^"074a1581-fd36-49e7-a756-03d30f378312",
order_by: [asc: c.pix_id],
select: {c.pix_id, c}
(elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
(elixir) lib/enum.ex:1418: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
(elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto) lib/ecto/repo/queryable.ex:124: Ecto.Repo.Queryable.execute/5
(ecto) lib/ecto/repo/queryable.ex:37: Ecto.Repo.Queryable.all/4
(elixir) lib/enum.ex:1314: Enum."-map/2-lists^map/1-0-"/2
Any ideas what I’m doing wrong?
Marked As Solved
polypush135
The solution was to set the type not only on the schema but also on the belongs_to.
belongs_to(:pix, Pix, type: :binary_id)
I would have assumed that setting the schema’s primary key type would have been enough as per the docs say.
https://hexdocs.pm/ecto/Ecto.Schema.html#module-schema-attributes
@foreign_key_type- configures the default foreign key type used bybelongs_toassociations. Defaults to:id;
Also Liked
NobbZ
I have no clue and I’m not using ecto, also I have not slept for 36+ hours now, but I’m pretty sure that someone will ask anyway, can you show your schemas and migrations involved?
polypush135
I think this was a case of miss reading the docs or possibly a bug on ecto.
I’ve since filed a bug for clarification since either the docs are miss leading or there is a bug.







