vinniefranco/exandra

Error casting '<< ... >>' to :binary_id

Closed this issue ยท 6 comments

Steps to reproduce.
On the repo https://github.com/cyberchitta/M.ai perform

mix ecto.drop
mix ecto.create
mix ecto.migrate
mix run priv/repo/seeds.exs

results in the stacktrace

** (Ecto.ChangeError) value `<<96, 78, 199, 171, 247, 83, 76, 152, 146, 66, 173, 187, 123, 148, 19, 228>>` for `Mai.Chat.Message.chat_id` in `insert` does not match type :binary_id
    (ecto 3.11.2) lib/ecto/repo/schema.ex:1049: Ecto.Repo.Schema.dump_field!/6
    (ecto 3.11.2) lib/ecto/repo/schema.ex:1058: anonymous fn/6 in Ecto.Repo.Schema.dump_fields!/5
    (stdlib 5.2) maps.erl:416: :maps.fold_1/4
    (ecto 3.11.2) lib/ecto/repo/schema.ex:1056: Ecto.Repo.Schema.dump_fields!/5
    (ecto 3.11.2) lib/ecto/repo/schema.ex:989: Ecto.Repo.Schema.dump_changes!/7
    (ecto 3.11.2) lib/ecto/repo/schema.ex:367: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
    (ecto 3.11.2) lib/ecto/repo/schema.ex:273: Ecto.Repo.Schema.insert!/4
    (elixir 1.16.2) lib/enum.ex:992: anonymous fn/3 in Enum.each/2
    (elixir 1.16.2) lib/enum.ex:4388: Enum.reduce_range/5
    (elixir 1.16.2) lib/enum.ex:2532: Enum.each/2
    (elixir 1.16.2) lib/enum.ex:992: anonymous fn/3 in Enum.each/2
    (elixir 1.16.2) lib/enum.ex:4388: Enum.reduce_range/5
    (elixir 1.16.2) lib/enum.ex:2532: Enum.each/2
    priv/repo/seeds.exs:22: (file)

However, <<96, 78, 199, 171, 247, 83, 76, 152, 146, 66, 173, 187, 123, 148, 19, 228>> is a 16 byte binary, so it should be a uuid i.e. a :binary_id, and the cast should work.
In fact, a very similar insert earlier in the same script (for inserting a new chat with a foreign key) does actually work.
UPDATE. Using exandra 0.10.2

Hi there, can you try using :uuid in your migration instead of :binary_id?

Examples are in the test suite: https://github.com/vinniefranco/exandra/blob/main/test%2Fexandra%2Fmigrations_test.exs#L37

@vinniefranco thanks for the response, and the link. I tried what you suggested, the migrations worked, but I'm still getting the same error. Are there schema examples I could look at? particularly ones with foreign keys?

UPDATE. On further research, it appears that foreign keys are not really used in cassandra. Even though ecto isn't complaining about the usage (which it did about 'references'), I guess no one is really exercising this code path.

Great question, btw. There is no such thing as foreign keys in Cassandra/Scylla. That said, try your schema uuid field definitions like so:

field(:my_uuid_field, Ecto.UUID)

We use that all over the place, and it works for us! I'm hoping it does for you as well ๐Ÿคž๐Ÿฝ

@vinniefranco thanks for the response, and the link. I tried what you suggested, the migrations worked, but I'm still getting the same error. Are there schema examples I could look at? particularly ones with foreign keys?

UPDATE. On further research, it appears that foreign keys are not really used in cassandra. Even though ecto isn't complaining about the usage (which it did about 'references'), I guess no one is really exercising this code path.

Yea, this is an adapter for Ecto, it doesn't prevent you from attempting RDBMS footguns everywhere... Only in the adapting parts. Datastax and Scylla have great courses on the differences in modeling. I recommend that because while there is a SQL-esque language - the differences are dramatic

Great question, btw. There is no such thing as foreign keys in Cassandra/Scylla. That said, try your schema uuid field definitions like so:

I was figuring that out even as you were writing ๐Ÿ˜ƒ .

field(:my_uuid_field, Ecto.UUID)

We use that all over the place, and it works for us! I'm hoping it does for you as well ๐Ÿคž๐Ÿฝ

Yes!! That worked. Thank you very much @vinniefranco

Great question, btw. There is no such thing as foreign keys in Cassandra/Scylla. That said, try your schema uuid field definitions like so:

I was figuring that out even as you were writing ๐Ÿ˜ƒ .

field(:my_uuid_field, Ecto.UUID)

We use that all over the place, and it works for us! I'm hoping it does for you as well ๐Ÿคž๐Ÿฝ

Yes!! That worked. Thank you very much @vinniefranco

sweeeeeeet! good luck!