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
我正在尝试使用此函数传递到Repo.all()
我不明白这个错误。我在另一个项目中有相同类型的代码,没有问题。这是版本之间的 Ecto 语法问题吗?对我来说这看起来很简单。我错过了什么?
最佳答案
在您编写的代码中,from
的第二个参数是这部分:where u.template_id == ^t_id
。 ArgumentError 试图告诉您将其转换为关键字列表。你可以像 [{:where, u.template == ^t_id}]
这样详细地写它,但大多数人会这样写:
def for _template_id(query \\ __MODULE__, t_id) do
from u in query,
where: u.template_id == ^t_id
end
这就是说您忘记了 where
之后的冒号。
关于elixir - Ecto 查询 from 的第二个参数必须是编译时关键字列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66308619/