Search code examples
elixirecto

Ecto query second argument to from must be a compile time keyword list


defmodule Arcade.TemplateMedia do
  use Ecto.Schema
  import Ecto.{Changeset, Query}


 def for_template_id(query \ MODULE, t_id) do
    from u in query,
    where u.template_id == ^t_id
  end



== Compilation error in file lib/arcade/template_media.ex ==
** (ArgumentError) second argument to from must be a compile time keyword list

I am trying to use this function to pass into a Repo.all()

I don't understand this error. I have the same kind of code in another project with no problem. Is this an Ecto syntax issue between versions? It looks straightforward to me. What am I missing?


Solution

  • In the code you have written, the second argument to from is this part: where u.template_id == ^t_id. The ArgumentError is trying to tell you to turn that into a keyword list. You can write it verbosely like [{:where, u.template == ^t_id}] but most people would write it like this:

    def for _template_id(query \\ __MODULE__, t_id) do
      from u in query,
      where: u.template_id == ^t_id
    end
    

    Which is all to say that you forgot the colon after the where.