Search code examples
postgresqluuidphoenix-frameworkecto

How to query Ecto 2.0 with UUID


I'm using Ecto 2.0.0-rc.4 And I have done this query that didn't work

def users do
  Repo.all(
    from u in User,
    where: u.id == "93fd15fb-fe21-4a59-813d-f80447417a23",
    select: u
  )
end

The id is one in the database the error it shows is

** (Postgrex.Error) ERROR (character_not_in_repertoire): invalid byte sequence for encoding "UTF8": 0x93
[debug] QUERY ERROR db=8.2ms queue=0.2ms
SELECT u0."id", u0."full_name", u0."email", u0."encrypted_password", u0."settings", u0."organizations", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."id" = '��^U��!JY�=�^DGAz#') []
    (ecto) lib/ecto/adapters/sql.ex:395: Ecto.Adapters.SQL.execute_and_cache/7
    (ecto) lib/ecto/repo/queryable.ex:127: Ecto.Repo.Queryable.execute/5
    (ecto) lib/ecto/repo/queryable.ex:40: Ecto.Repo.Queryable.all/4

Also have tried converting {:ok, id} = Ecto.UUID.dump "93fd15fb-fe21-4a59-813d-f80447417a23" to bitstring and query u.id == ^id but did not work-

Is this and Ecto issue.


Solution

  • It was my mistake; just needed to add the following import to my model (i.e: import Ecto.Query):

      def all(org_id) do
        Repo.all(
          from l in Ledger,
          where: l.organization_id == ^org_id
        )
      end