wafcio

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}

Showing Posts 1 to 10

zimt28

zimt28

Have you tried it without the ^?

wafcio

wafcio OP

yes, without ^ I am receiving

 value `"de397199-e3dc-4974-9924-4ce1fa93b1d6"` cannot be dumped to type :id. Or the value is incompatible or it must be interpolated (using ^) so it may be cast accordingly in query
NobbZ

NobbZ

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

wafcio OP

I have removed lines @primary_key {:id, :binary_id, autogenerate: false} from models. But I am still receiving error:

** (Ecto.Query.CastError) deps/ecto/lib/ecto/association.ex:399: value `<<222, 57, 113, 153, 227, 220, 73, 116, 153, 36, 76, 225, 250, 147, 177, 214>>` in `where` cannot be cast to type :id in query:

from p in Catalog.Product,
  where: p.publisher_id == ^<<222, 57, 113, 153, 227, 220, 73, 116, 153, 36, 76, 225, 250, 147, 177, 214>>,
  order_by: [asc: p.publisher_id],
  select: {p.publisher_id, v}
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.

wafcio

wafcio OP

when I replaced

@primary_key {:id, :binary_id, autogenerate: false}

with

@primary_key {:id, Ecto.UUID, autogenerate: false}

Code:

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

return

** (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 p in Catalog.Product,
  where: p.publisher_id == ^"de397199-e3dc-4974-9924-4ce1fa93b1d6",
  order_by: [asc: p.publisher_id],
  select: {p.publisher_id, v}
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

wafcio

wafcio OP

I set

@foreign_key_type Ecto.UUID

in each model under @primary_key and it works.

Thanks @josevalim, @NobbZ

boddhisattva

boddhisattva

I’m facing a weird issue with a get user end point that uses a uuid as an id. I’m using Ecto 2.2.8. My end point url looks like: http://localhost:4000/v1/accounts/da192848-0735-4382-9407-2bc32cb466cf

  1. For a correct UUID(E.g., UUID: da192848-0735-4382-9407-2bc32cb466ce), I get a proper account info as part of the json.

  2. 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:


{

"status": "FAILURE",

"errors": [

{

"message": "Requested resource not found",

"code": "NOT_FOUND"

}

]

}

  1. For an incorrect UUID(let’s say with lesser characters), I get the below response:

Ecto.Query.CastError at GET /v1/accounts/da192

deps/ecto/lib/ecto/repo/queryable.ex:348: value `"da192"` in `where` cannot be cast to type Ecto.UUID in query:

from a in MyApp.Users.Account,

where: a.id == ^"da192",

select: a



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/da192 which uses a da192 as it’s UUID.

My migration has the column name as binary_id like : add :id, :binary_id, primary_key: true. Should I be using add :id, :uuid, primary_key: true ? Or is there something else that I need to do in order to fix this ?

NobbZ

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…

Where Next? Top

Trending in Questions Top

katta
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
achenet
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
peck
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
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
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
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
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 Top

mikehostetler
ReqLLM v1.28.0 has been released. Features: add StepFun chat and audio providers (#1087) by mikehostetler openai: honour image deta...
New
GenericJam
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
budgie
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
KristerV
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
mudasobwa
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews