Closed joshchernoff closed 5 years ago
Can you please post your migration and schemas and can you make sure that you are calling @foreign_key_type
in the schema that calls belongs_to
? Thank you.
Btw, here is a test case that shows this feature is working: https://github.com/elixir-ecto/ecto/blob/master/test/ecto/schema_test.exs#L107-L126 - so more information to reproduce the issue is welcome.
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
...
end
defmodule PolymorphicProductions.Social.Comment do
...
alias PolymorphicProductions.Social.Pix
schema "comments" do
...
belongs_to(:pix, Pix, type: :binary_id)
...
end
...
end
Migrations:
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)))
timestamps()
end
create(index(:comments, [:pix_id]))
end
end
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
I should clarify the above works. but only when I set the type via belongs_to(:pix, Pix, type: :binary_id)
@joshchernoff it seems you are not setting @foreign_key_type :binary_id
in the module that invokes the belongs_to
.
the @foreign_key_type for the PolymorphicProductions.Social.Comment
should be the default :id
the @foreign_key_type for the PolymorphicProductions.Social.Pix
should be :binary_id
@joshchernoff right, I am saying that won't work. When you set @foreign_key_type
, it affects the belongs_to
calls in that module, it doesn't affect how other modules will belong to you. Generally speaking, the option is only useful when you change the foreign key type across the whole system.
That seem odd to me, why would you want to state the type of a different module's foreign_key_type via declaring the current module's @foreign_key_type for a belongs_to. Would that not also change the current module's @foreign_key_type ? what if it is different vs its association?
Right, but it simply cannot work on the other way because that could trigger deadlocks during compilation, as modules would have to query each other for the type. So as said above:
Generally speaking, the option is only useful when you change the foreign key type across the whole system.
It is basically a mechanism for flipping the default generally. If only part of your system wants to change it, then it is better to do it as an option per association.
Ok thank you for your help, that makes better sense. Maybe more could be done to help explain that in the docs. I think its easy to infer what I believed to be true. I'm not sure how I would word it.
I just pushed a commit along those lines, if you feel they could be improved, please do send another PR! --
José Valimwww.plataformatec.com.br http://www.plataformatec.com.br/Founder and Director of R&D
When reading the docs for schema attributes specifically for
@foreign_key_type
https://hexdocs.pm/ecto/Ecto.Schema.html#module-schema-attributesIt states:
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:
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):
Database and version (PostgreSQL 10.):
Ecto version (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