wafcio
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}
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
ReqLLM v1.28.0 has been released.
Features:
add StepFun chat and audio providers (#1087) by mikehostetler
openai: honour image deta...
New
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
zimt28
Have you tried it without the
^?wafcio
yes, without
^I am receivingNobbZ
You need to use the raw binary form of the UUID and not ist textual representation as far as I know.
So it is something along
<<0xde, 0x39, 0x71, 0x99, ...>>.At least, thats the representation on my little toy project when I take a look at the logged queries.
wafcio
I have removed lines
@primary_key {:id, :binary_id, autogenerate: false}from models. But I am still receiving error:josevalim
If you removed that line from your schema, then it is going to expect integers. You have three options:
It seems you want 3.
wafcio
when I replaced
with
Code:
return
NobbZ
You have to set the type of your foreign key in your scheme:
wafcio
I set
in each model under
@primary_keyand it works.Thanks @josevalim, @NobbZ
boddhisattva
I’m facing a weird issue with a get user end point that uses a
uuidas an id. I’m using Ecto 2.2.8. My end point url looks like:http://localhost:4000/v1/accounts/da192848-0735-4382-9407-2bc32cb466cfFor a correct UUID(E.g., UUID:
da192848-0735-4382-9407-2bc32cb466ce), I get a proper account info as part of the json.For an incorrect UUID(let’s say for the same uuid length but some character changed E.g., UUID:
da192848-0735-4382-9407-2bc32cb466cf), I get an incorrect json and like a below json response:I tried making the changes recommended above. How can I get the response as with case 2 above for a request url like:
v1/accounts/da192which uses ada192as it’s UUID.My migration has the column name as
binary_idlike :add :id, :binary_id, primary_key: true. Should I be usingadd :id, :uuid, primary_key: true? Or is there something else that I need to do in order to fix this ?NobbZ
As you haven’t shown much code, I can only guess, but it seems as if you are not checking the given UUID for validity. So when you have invalid UUIDs you can replace them with a valid but known to be unused one. It seems as if you were using UUID 4, so just replace invalid UUIDs with a fixed UUID 1 and the database will never find it.
Doing it this way will ensure that invalid UUIDs are always treatened as if they were inexistant. I’d prefer to return a proper error, telling the user that he sent you garbage.
Also I think it is strange to have UUIDs as part of the URL, they are hard to type for humans…