3
votes

I'm trying to use uuid's with ecto. I am able to query all, but I get an unable to encode value error with Repo.get.

defmodule Rocket.Model do
  defmacro __using__(_) do
    quote do
      use Ecto.Model
      @primary_key {:id, :uuid, []}
      @foreign_key_type :uuid
    end
  end
end

defmodule Rocket.User do
  use Rocket.Model

  schema "users" do
    field :created_at, :datetime
    field :name, :string
    field :email, :string
    field :password, :string
    field :timezone, :string
  end
end


iex(37)> Rocket.Repo.get(Rocket.User,"00000000-0000-0000-0000-00000000011b") 
21:51:58.291 [debug] SELECT u0."id", u0."created_at", u0."name", u0."email", u0."password", u0."timezone" FROM "users" AS u0 WHERE (u0."id" = $1) (0.4ms)
** (Postgrex.Error) unable to encode value `"00000000-0000-0000-0000-00000000011b"` as type uuid
      (ecto) lib/ecto/adapters/sql/worker.ex:18: Ecto.Adapters.SQL.Worker.query!/4
      (ecto) lib/ecto/adapters/sql.ex:186: Ecto.Adapters.SQL.use_worker/3
    (rocket) web/models/repo.ex:2: Rocket.Repo.log/2
      (ecto) lib/ecto/adapters/sql.ex:315: Ecto.Adapters.SQL.all/5
      (ecto) lib/ecto/repo/queryable.ex:20: Ecto.Repo.Queryable.all/4
      (ecto) lib/ecto/repo/queryable.ex:43: Ecto.Repo.Queryable.one/4
iex(37)> 

Using all works, but the uuid is not represented as a string in the console nor in json decoding:

iex(38)> user = Rocket.Repo.all(Rocket.User) |> List.first
21:58:55.450 [debug] SELECT u0."id", u0."created_at", u0."name", u0."email", u0."password", u0."timezone" FROM "users" AS u0 (1.5ms)
%Rocket.User{created_at: %Ecto.DateTime{day: 3, hour: 2, min: 16, month: 1,
  sec: 3, year: 2015}, email: "[email protected]",
 id: <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 29>>, name: "Joel",
 password: "$1$E0eFWgDt$YgQTEByVT7prYkT2IqBjL1", timezone: nil}
iex(39)> IO.puts Poison.Encoder.encode(udb,[])  
{"timezone":null,"password":"$1$E0eFWgDt$YgQTEByVT7prYkT2IqBjL1","name":"Joel","id":"\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u001D","email":"[email protected]","created_at":{"year":2015,"sec":3,"month":1,"min":16,"hour":2,"day":3}}
:ok
1

1 Answers

4
votes

UUIDs are just 16 bytes so what you get from all/1 is actually correct. If you want to work with the verbose representation of UUIDs, you can explicitly convert it or use a custom type to automatically do the work for you when loading and dumping data to the database. I have talked about custom types in this answer.