(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?

2 Likes

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?

4 Likes

@polygonpusher Probably same problem as in:

I had read that but I’m not sure what the solution was for that. Also heres my scheme and migration.

defmodule PolymorphicProductions.Social.Pix do
   ...
  @primary_key {:id, :binary_id, autogenerate: true}
  @foreign_key_type :binary_id

  schema "pics" do
    has_many(:comments, PolymorphicProductions.Social.Comment)
  end

defmodule PolymorphicProductions.Social.Comment do
   ...
  alias PolymorphicProductions.Social.Pix

  schema "comments" do
    belongs_to(:pix, Pix)
  end

Migrations:

defmodule PolymorphicProductions.Repo.Migrations.CreatePics do
  use Ecto.Migration

  def change do
    create table(:pics, primary_key: false) do
      add(:id, :binary_id, primary_key: true)
      ...
    end
  end
end

defmodule PolymorphicProductions.Repo.Migrations.CreateComments do
  use Ecto.Migration

  def change do
    create table(:comments) do
      ...
      add(:pix_id, references(:pics, on_delete: :nothing, type: :uuid))
      ...
    end

    create(index(:comments, [:pix_id]))
  end
end

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 by belongs_to associations. Defaults to :id ;

4 Likes

I did not used it before, so I’m not sure. Maybe you should set this attribute in PolymorphicProductions.Social.Comment's schema instead. If none of them works then you should create issue about it, because docs would not match real work. Also if docs are not enough clear then consider creating an issue describing which part was confusing for you or just PR with changes in documentation which you think that would be easier to understand for people…

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.

https://github.com/elixir-ecto/ecto/issues/2806

1 Like

Ok so it looks as if the doc where a little miss leading. there has been a commit to address that.
https://github.com/elixir-ecto/ecto/commit/e8c935333b33a632bd69f92459a6db2209415a73

For anyone here reading along @foreign_key_type will change the belongs_to type but only for the belongs to inside the same module the @foreign_key_type was used. This was explained to me as simply used for more a global change vs just module per module change. the belongs_to will not infer the type for of a different module’s type as explained by @josevalim here https://github.com/elixir-ecto/ecto/issues/2806#issuecomment-437461962