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
NobbZ
November 9, 2018, 5:58am
2
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
Eiji
November 9, 2018, 6:22am
3
@polygonpusher Probably same problem as in:
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…
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
;
6 Likes
Eiji
November 9, 2018, 6:05pm
6
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.
opened 06:07PM - 09 Nov 18 UTC
closed 07:04PM - 09 Nov 18 UTC
When reading the docs for schema attributes specifically for `@foreign_key_type`…
https://hexdocs.pm/ecto/Ecto.Schema.html#module-schema-attributes
It states:
> @foreign_key_type - configures the default foreign key type used by belongs_to associations. Defaults to :id;
I found this not be true and that not only do I have to set the @foreign_key_type but also I would have to set the belongs_to type explicitly if its not the default :id type.
The type of error I get was:
```
** (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:
```
I had initaliy started this as a question on the elixir forums found here:
https://elixirforum.com/t/ecto-query-casterror-value-some-uuid-in-where-cannot-be-cast-to-type-id-in-query/17816
As you can see my solution was to explicitly set the belongs_to type as it was not inferred based on setting the schema's type.
### Environment
* Elixir version (elixir -v):
```Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
Elixir 1.7.0 (compiled with Erlang/OTP 20)```
* Database and version (PostgreSQL 10.):
* Ecto version (mix deps):
```
❯ mix deps
* parse_trans 3.3.0 (Hex package) (rebar3)
locked at 3.3.0 (parse_trans) 09765507
ok
* mimerl 1.0.2 (Hex package) (rebar3)
locked at 1.0.2 (mimerl) 993f9b0e
ok
* file_system 0.2.6 (Hex package) (mix)
locked at 0.2.6 (file_system) fd4dc3af
ok
* connection 1.0.4 (Hex package) (mix)
locked at 1.0.4 (connection) a1cae722
ok
* metrics 1.0.1 (Hex package) (rebar3)
locked at 1.0.1 (metrics) 25f094de
ok
* unicode_util_compat 0.4.1 (Hex package) (rebar3)
locked at 0.4.1 (unicode_util_compat) d869e4c6
ok
* idna 6.0.0 (Hex package) (rebar3)
locked at 6.0.0 (idna) 689c46cb
ok
* gettext 0.16.0 (Hex package) (mix)
locked at 0.16.0 (gettext) 4a7e9040
ok
* ranch 1.6.2 (Hex package) (rebar3)
locked at 1.6.2 (ranch) 6db93c78
ok
* poolboy 1.5.1 (Hex package) (rebar)
locked at 1.5.1 (poolboy) 6b461639
ok
* decimal 1.5.0 (Hex package) (mix)
locked at 1.5.0 (decimal) b0433a36
ok
* jason 1.1.2 (Hex package) (mix)
locked at 1.1.2 (jason) b03dedea
ok
* poison 3.1.0 (Hex package) (mix)
locked at 3.1.0 (poison) d9eb6366
ok
* ssl_verify_fun 1.1.4 (Hex package) (mix)
locked at 1.1.4 (ssl_verify_fun) f0eafff8
ok
* combine 0.10.0 (Hex package) (mix)
locked at 0.10.0 (combine) eff8224e
ok
* elixir_make 0.4.2 (Hex package) (mix)
locked at 0.4.2 (elixir_make) 332c649d
ok
* bcrypt_elixir 1.1.1 (Hex package) (mix)
locked at 1.1.1 (bcrypt_elixir) 6b5560e4
ok
* argon2_elixir 1.3.1 (Hex package) (mix)
locked at 1.3.1 (argon2_elixir) 02a3d55a
ok
* comeonin 4.1.1 (Hex package) (mix)
locked at 4.1.1 (comeonin) c7304fc2
ok
* mogrify 0.6.1 (Hex package) (mix)
locked at 0.6.1 (mogrify) de1b5275
ok
* certifi 2.4.2 (Hex package) (rebar3)
locked at 2.4.2 (certifi) 75424ff0
ok
* hackney 1.14.3 (Hex package) (rebar3)
locked at 1.14.3 (hackney) b5f6f5dc
ok
* tzdata 0.5.19 (Hex package) (mix)
locked at 0.5.19 (tzdata) 7962a399
ok
* timex 3.4.1 (Hex package) (mix)
locked at 3.4.1 (timex) e63fc1a3
ok
* sweet_xml 0.6.5 (Hex package) (mix)
locked at 0.6.5 (sweet_xml) dd9cde44
ok
* ex_aws 2.1.0 (Hex package) (mix)
locked at 2.1.0 (ex_aws) b9265152
ok
* ex_aws_s3 2.0.1 (Hex package) (mix)
locked at 2.0.1 (ex_aws_s3) 9e09366e
ok
* db_connection 1.1.3 (Hex package) (mix)
locked at 1.1.3 (db_connection) 89b30ca1
ok
* phoenix_pubsub 1.1.1 (Hex package) (mix)
locked at 1.1.1 (phoenix_pubsub) 6668d787
ok
* social_parser 2.0.0 (Hex package) (mix)
locked at 2.0.0 (social_parser) 67854728
ok
* cowlib 2.6.0 (Hex package) (rebar3)
locked at 2.6.0 (cowlib) 8aa629f8
ok
* cowboy 2.5.0 (Hex package) (rebar3)
locked at 2.5.0 (cowboy) 4ef3ae06
ok
* uuid 1.1.8 (Hex package) (mix)
locked at 1.1.8 (uuid) e22fc044
ok
* mime 1.3.0 (Hex package) (mix)
locked at 1.3.0 (mime) 5e8d45a3
ok
* postgrex 0.13.5 (Hex package) (mix)
locked at 0.13.5 (postgrex) 3d931aba
ok
* ecto 2.2.11 (Hex package) (mix)
locked at 2.2.11 (ecto) 4bb8f117
ok
* artificery 0.2.6 (Hex package) (mix)
locked at 0.2.6 (artificery) f6029097
ok
* distillery 2.0.10 (Hex package) (mix)
locked at 2.0.10 (distillery) e9f1f1d3
ok
* edeliver 1.6.0 (Hex package) (mix)
locked at 1.6.0 (edeliver) 8bfdde1b
ok
* plug_crypto 1.0.0 (Hex package) (mix)
locked at 1.0.0 (plug_crypto) 18e49317
ok
* plug 1.7.1 (Hex package) (mix)
locked at 1.7.1 (plug) 8516d565
ok
* plug_cowboy 2.0.0 (Hex package) (mix)
locked at 2.0.0 (plug_cowboy) ab0c9272
ok
* phoenix 1.4.0-rc.3 (https://github.com/phoenixframework/phoenix.git) (mix)
locked at 4ce6c55
ok
* phoenix_live_reload 1.1.7 (Hex package) (mix)
locked at 1.1.7 (phoenix_live_reload) 425fff57
ok
* phoenix_html 2.12.0 (Hex package) (mix)
locked at 2.12.0 (phoenix_html) 1fb3c2e4
ok
* phauxth 2.0.0-rc.0 (https://github.com/riverrun/phauxth.git) (mix)
locked at bdff0dc
ok
* bamboo 1.1.0 (Hex package) (mix)
locked at 1.1.0 (bamboo) ecbdc851
ok
* sentry 6.4.2 (Hex package) (mix)
locked at 6.4.2 (sentry) 71b3cf8c
ok
* kerosene 0.8.1 (Hex package) (mix)
locked at 0.8.1 (kerosene) 05abab9d
ok
* phoenix_ecto 3.4.0 (Hex package) (mix)
locked at 3.4.0 (phoenix_ecto) 91cd3942
ok
```
* Operating system: OSX
1 Like
Ok so it looks as if the doc where a little miss leading. there has been a commit to address that.
committed 06:41PM - 09 Nov 18 UTC
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 belongs_to does not infer type based on @foreign_key_type as docs would suggest. · Issue #2806 · elixir-ecto/ecto · GitHub