polypush135

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

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.

@foreign_key_type - configures the default foreign key type used by belongs_to associations. Defaults to :id ;

Also Liked

NobbZ

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

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.

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

Where Next?

Popular in Questions Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31265 143
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127089 1222
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54120 245
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement