logo
down
shadow

Querying a table with UUID using Ecto.Query gives Ecto.Query.CastError


Querying a table with UUID using Ecto.Query gives Ecto.Query.CastError

By : Devarajan D
Date : October 18 2020, 01:08 AM
should help you out I find the problem you're experiencing is a bit confusing at first, but I think it works quite correctly - the problem is in how you're querying the database.
If you have your query ready, you need to use Repo.all(query), so your code should be:
code :
q = from s in Thing, where: s.id == "ba34d9a0-889f-4999-ac23-f04c7183f2ba", select: s
Repo.all(q)
Repo.get!(Thing, "ba34d9a0-889f-4999-ac23-f04c7183f2ba")
** (Ecto.Query.CastError) /project/deps/ecto/lib/ecto/repo/queryable.ex:341: value `#Ecto.Query<from o in App.Thing, where: o.id == "ba34d9a0-889f-4999-ac23-f04c7183f2ba", select: o>` in `where` cannot be cast to type Ecto.UUID in query:


Share : facebook icon twitter icon
Phoenix/Elixir/Ecto.CastError: Value `"new"` in `where` cannot be cast to type :id in query: from u in MyApp.U

Phoenix/Elixir/Ecto.CastError: Value `"new"` in `where` cannot be cast to type :id in query: from u in MyApp.U


By : nine01223
Date : March 29 2020, 07:55 AM
Does that help I'm using Programming Phoenix, adding a New User function to my app: , Fixed by deleting this line:
code :
get "/users/:id", Microflow.UserController, :show
resources "/users", Microflow.UserController 
How to query Ecto 2.0 with UUID

How to query Ecto 2.0 with UUID


By : L0g4n
Date : March 29 2020, 07:55 AM
hope this fix your issue It was my mistake; just needed to add the following import to my model (i.e: import Ecto.Query):
code :
  def all(org_id) do
    Repo.all(
      from l in Ledger,
      where: l.organization_id == ^org_id
    )
  end
(Ecto.QueryError) deps/ecto/lib/ecto/association.ex:399: field `App.Location.post_id` in `where` does not exist in the s

(Ecto.QueryError) deps/ecto/lib/ecto/association.ex:399: field `App.Location.post_id` in `where` does not exist in the s


By : Jijo
Date : March 29 2020, 07:55 AM
it helps some times If you have a foreign key for another table in a table, the relationship you need to use is belongs_to, not has_one. This should work:
code :
schema "posts" do
  ...
  belongs_to :location, App.Location
end
Using a table alias/query expression in an Ecto fragment?

Using a table alias/query expression in an Ecto fragment?


By : GM74
Date : March 29 2020, 07:55 AM
help you fix your problem The solution is not perfect because it requires listing all fields explicitly and also doesn't let you exclude fields from the resulting JSON.
code :
 # ids=[1234] 
 from(p in Profile,
   join: link in FolderMembership, on: link.profile_id == p.id,
   select: [link.folder_id, fragment("json_agg((?, ?, ?)::profiles) as members", p.id, p.name, p.created_at)],
   group_by: link.folder_id,
   where: link.folder_id in ^ids
 ) 
 |> Repo.all
defmodule Magic do
  import Ecto.Query
  alias Badging.{Badge, Repo}

  @fields Badge.__schema__(:fields)
  @source Badge.__schema__(:source)
  @questions Enum.map_join(@fields, ", ", fn _ -> "?" end)
  @json_agg "json_agg((#{@questions})::#{@source})"

  def run do
    fields = Badge.__schema__(:fields)
    source = Badge.__schema__(:source)
    questions = Enum.map_join(fields, ", ", fn _ -> "?" end)
    json_agg = "json_agg((#{questions})::#{source})"

    from(
      b in Badge,
      select: [
        b.id,
        fragment(
          "json_agg((?, ?, ?, ?, ?, ?, ?, ?, ?)::badges)",
          b.id,
          b.identifier,
          b.subject,
          b.status,
          b.color,
          b.svg,
          b.svg_downloaded_at,
          b.inserted_at,
          b.updated_at
        )
      ],
      group_by: b.id
    ) |> Repo.all
  end
end
defmodule Magic do
  import Ecto.Query
  alias Badging.{Badge, Repo}

  fields = Badge.__schema__(:fields)
  source = Badge.__schema__(:source)
  questions = Enum.map_join(fields, ", ", fn _ -> "?" end)
  @json_agg "json_agg((#{questions})::#{@source})"

  def run do
    from(
      b in Badge,
      select: [
        b.id,
        fragment(
          @json_agg,
          field(b, :id), # or just b.id
          b.identifier,
          b.subject,
          b.status,
          b.color,
          b.svg,
          b.svg_downloaded_at,
          b.inserted_at,
          b.updated_at
        )
      ],
      group_by: b.id
    ) |> Repo.all
  end
end
Getting Ecto.Query.CastError when querying in phoenix

Getting Ecto.Query.CastError when querying in phoenix


By : sempernoob
Date : March 29 2020, 07:55 AM
seems to work fine Repo.get!/2 fetches a record by its primary key but you're passing a query to it. You need to use Repo.one!/1:
code :
Repo.one!(query)
shadow
Privacy Policy - Terms - Contact Us © soohba.com