wafcio

wafcio

UUID and problem with loading related models

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 :id, :uuid, primary_key: true, default: fragment("uuid_generate_v4()")
      add :publisher_id, references(:publishers, type: :uuid)
      add :name, :string, null: false
    end
  end
end

models:

defmodule Catalog.Publisher do
  use Catalog.Web, :model

  @primary_key {:id, :binary_id, autogenerate: false}
  schema "publishers" do
    field :name, :string
    has_many :products, Catalog.Product
  end
end

defmodule Catalog.Product do
  use Catalog.Web, :model

  @primary_key {:id, :binary_id, autogenerate: false}
  schema "products" do
    field :name, :string
    belongs_to :publisher, Catalog.Publisher
  end
end

And loading publisher with products:

query = from _ in Catalog.Publisher, limit: 1
publisher = query |> Catalog.Repo.one |> Catalog.Repo.preload(:products)

Unfortunately there is something wrong because I am receiving error:

** (Ecto.Query.CastError) deps/ecto/lib/ecto/association.ex:399: value `"de397199-e3dc-4974-9924-4ce1fa93b1d6"` in `where` cannot be cast to type :id in query:

from v in Catalog.Product,
  where: v.publisher_id == ^"de397199-e3dc-4974-9924-4ce1fa93b1d6",
  order_by: [asc: v.publisher_id],
  select: {v.publisher_id, v}

Most Liked

NobbZ

NobbZ

Yes and no… The result of the query depends on how your database handles invalid input. But I think most databases error out on invalid input. Passing an ill-formed UUID is like passing a string when an integer is expected.

josevalim

josevalim

Creator of Elixir

If you removed that line from your schema, then it is going to expect integers. You have three options:

  1. Use :integer keys and then pass integer values
  2. Use :binary_id and pass binary values
  3. Use Ecto.UUID and pass actual UUIDs

It seems you want 3.

NobbZ

NobbZ

You have to set the type of your foreign key in your scheme:

:type - Sets the type of automatically defined :foreign_key. Defaults to: :id and can be set per schema via @foreign_key_type

Where Next?

Popular in Questions Top

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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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

Other popular topics Top

lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
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
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement