Alvaro
Alvaro

Reputation: 77

Error trying to execute query in Elixir with Ecto

I have an Elixir app that uses Ecto in order to execute queries in Postgres. I have this Ecto schema:

defmodule Person do

  use Ecto.Schema

  schema "person" do
    field :name, :string
  end

end

I'm trying to make a simple select in Elixir:

query = from Person
result = Repo.all(query)

but after executing that code, I see this:

** (FunctionClauseError) no function clause matching in Ecto.Adapters.Postgres.Connection.expr/3

The following arguments were given to Ecto.Adapters.Postgres.Connection.expr/3:

    # 1
    {{:., [], [{:&, [], [0]}, "name"]}, [], []}

    # 2
    {{[34, "Person", 34], [84 | "0"],
      Person}, []}

    # 3
    #Ecto.Query<from c0 in Person,
     select: c0>

Attempted function clauses (showing 10 out of 33):

    defp expr(-{:^, [], [ix]}-, _sources, _query)
    defp expr(-{{:., _, [{:parent_as, _, [as]}, field]}, _, []}-, _sources, query) when -is_atom(field)-
    defp expr({{:., _, [{:&, _, [idx]}, field]}, _, []}, sources, _query) when -is_atom(field)-
    defp expr(-{:&, _, [idx]}-, sources, _query)
    defp expr(-{:in, _, [_left, []]}-, _sources, _query)
    defp expr(-{:in, _, [left, right]}-, sources, query) when -is_list(right)-
    defp expr(-{:in, _, [left, {:^, _, [ix, _]}]}-, sources, query)
    defp expr(-{:in, _, [left, %Ecto.SubQuery{} = subquery]}-, sources, query)
    defp expr(-{:in, _, [left, right]}-, sources, query)
    defp expr(-{:is_nil, _, [arg]}-, sources, query)
    ...
    (23 clauses not shown)

(ecto_sql 3.7.2) lib/ecto/adapters/postgres/connection.ex:590: Ecto.Adapters.Postgres.Connection.expr/3
(ecto_sql 3.7.2) lib/ecto/adapters/postgres/connection.ex:1284: Ecto.Adapters.Postgres.Connection.intersperse_map/4
(ecto_sql 3.7.2) lib/ecto/adapters/postgres/connection.ex:340: Ecto.Adapters.Postgres.Connection.select/3
(ecto_sql 3.7.2) lib/ecto/adapters/postgres/connection.ex:113: Ecto.Adapters.Postgres.Connection.all/2
(ecto_sql 3.7.2) lib/ecto/adapters/postgres.ex:102: Ecto.Adapters.Postgres.prepare/2
(ecto 3.7.2) lib/ecto/query/planner.ex:180: Ecto.Query.Planner.query_without_cache/4
(ecto 3.7.2) lib/ecto/query/planner.ex:150: Ecto.Query.Planner.query_prepare/6
(ecto 3.7.2) lib/ecto/query/planner.ex:125: Ecto.Query.Planner.query_with_cache/7

Also same result with Repo.all.

This are the versions that I'm using: Elixir: 1.13 ecto_sql: 3.7.2 postgrex: 0.16.2

Does anyone has any idea what is making this error?

Thanks

Upvotes: 0

Views: 523

Answers (1)

Alvaro
Alvaro

Reputation: 77

I finally figured out the problem: I have a table in a Postgres database, that contains two fields: person_age_123 and person_name_123.

So on my schema on Elixir, I was trying to map those database column names to a more "friendly" field names, like age and name.

So my Elixir schema was the following:

defmodule EctoApp.Person do

  use Ecto.Schema

  schema "person" do
    field :name, :string, [source: "person_name_123"]
    field :age, :integer, [source: "person_age_123"]
  end

end

and the error was because I was using " instead of :, so I think that the idea is to include atoms and not strings.

Anyway I thing that Elixir could show a more friendly message error, since I figure out the problem after try a lot of things.

Upvotes: 0

Related Questions