Mouad Bahri
Mouad Bahri

Reputation: 13

Ecto.QueryError : invalid keyword list in query

I keep getting this error and I don't know what it refers to or how to solve it.

enter image description here

The piece of code that might help:

defmodule WeatherWeb.PageController do
  use WeatherWeb, :controller

  def index(conn, _params) do
    render(conn, "index.html")
  end

  def fetch(conn, name) do
    case Weather.get_by(name: name) do
      %Weather.Location{} = locations ->
        json(conn, Map.take(locations, Weather.Location.fields()))

      nil ->
        IO.inspect("Weather not found for '#{name}'")
    end
  end
end

Here is the get_by function:

def get_by(name) do
    Weather.Location
    |> Repo.get_by(name: name) 
end

and the Weather.Location Module:

defmodule Weather.Location do
  use Ecto.Schema

  schema "Locations" do
    field :name, :string
    field :temp, :decimal
    field :pressure, :integer
    field :humidity, :integer
    field :windspeed, :decimal
    field :winddeg, :decimal

    timestamps()
  end

  def changeset(location, params \\ %{}) do
    location
    |> Ecto.Changeset.cast(params, [:name, :temp, :pressure, :humidity, :windspeed, :winddeg])
    |> Weather.Repo.insert()
  end

  def fields() do
    __MODULE__.__schema__(:fields)
  end
end

I'm new to Elixir and this is my first post on Stackoverflow, so I'm sorry if I missed something.

Upvotes: 1

Views: 164

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 120990

You have posted the snippet from the wrong file. The issue happens in Weather.get_by/1 which calls a query on Weather.Location (what might have been seen from the stacktrace if you bothered to post it.)

Either it has a query by name hardcoded, and then you’d need to call it as Weather.get_by(name), or the query is poorly constructed and you need to pass a keyword value instead of the whole keyword list there.

Upvotes: 1

Related Questions