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
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.
3
josevalim
Creator of Elixir
If you removed that line from your schema, then it is going to expect integers. You have three options:
- Use :integer keys and then pass integer values
- Use :binary_id and pass binary values
- Use Ecto.UUID and pass actual UUIDs
It seems you want 3.
2
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
1
Popular in Questions
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
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
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
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
i’m a new one to elixir
which editor can i use
vs code? or atom?
Thanks! :smiley:
New
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
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
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
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
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
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
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help.
Where are they similar?
Where do they differ the m...
New
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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
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
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
I would like to know what is the best IDE for elixir development?
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








