I am writing an application in Elixir with Postgres as the data store. In Ecto, I defined this entity:
defmodule MyModule.User do
use Ecto.Schema
import Ecto.Changeset
@primary_key {:id, :binary_id, autogenerate: true}
schema "users" do
field :email, :string
end
@fields ~w(email)
def changeset(data, params \\ %{}) do
data
|> cast(params, @fields)
|> validate_required([:email])
|> validate_length(:email, max: 128)
|> unique_constraint(:email)
end
end
Then, I can save a record in the db in iex
-terminal by typing:
iex> user1 = MyModule.User.changeset(%MyModule.User{}, %{email: "[email protected]"})
iex> MyModule.Repo.insert!(user1)
Everything runs ok and the record gets saved. But when I try to retrieve it with Repo.get()
:
iex> MyModule.Repo.get(MyModule.User, 0)
I get this error:
** (Ecto.Query.CastError) deps/ecto/lib/ecto/repo/queryable.ex:320:
value `0` in `where` cannot be cast to type :binary_id in query:
from u in ScorecardBackend.User,
where: u.id == ^0,
select: u
(elixir) lib/enum.ex:1755: Enum."-reduce/3-lists^foldl/2-0-"/3
(elixir) lib/enum.ex:1325: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
My question: Is there a way to type :binary_id
literals in the iex
-command line? Thanks a lot!