elixir - Ecto 查询 from 的第二个参数必须是编译时关键字列表

标签 elixir ecto

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/

相关文章:

Elixir Repo.insert_all/2 和唯一约束

dictionary - Elixir 将结构转换为映射

sockets - Phoenix Channels - 每个插槽有多个 channel

elixir - Phoenix/Elixir - validate_format() 整数失败

validation - 为什么 Changeset.change 在 Elixir 中跳过验证?

erlang - 如何在 Erlang/Elixir 中动态定义函数

elixir - 如何在一个Supervisor下初始化2个DynamicSupervisor

database - 使用 Elixir 选择 Postgres ltree 的直接 child

postgresql - 如何使用 PostgreSQL 在 Ecto 中使用点列类型

elixir - 如何使用 Ecto 测试唯一性约束