Elixir 使用字符串查询Repo

Elixir 使用字符串查询Repo,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,我正在使用Phoenix和Exto通过主键查询数据库中的单个记录。所有文档/示例都显示了在Phoenix控制器中的用法: def show(conn, %{"id" => id}) do m = Repo.get(MyModel, id) ... end 但是,Phoenix中的所有参数都是字符串,因此这会抛出一个**(exto.InvalidModel)模型App.MyModel验证失败,因为字段id的类型为string,但类型应为integer。我一直在我的控制器中解决这一

我正在使用Phoenix和Exto通过主键查询数据库中的单个记录。所有文档/示例都显示了在Phoenix控制器中的用法:

def show(conn, %{"id" => id}) do
  m = Repo.get(MyModel, id)

  ...
end
但是,Phoenix中的所有参数都是字符串,因此这会抛出一个
**(exto.InvalidModel)模型App.MyModel验证失败,因为字段id的类型为string,但类型应为integer
。我一直在我的控制器中解决这一问题,方法如下:

def show(conn, %{"id" => id}) do
   m = String.to_integer(id) |> find_my_model_by_id

  ...
end

defp find_my_model_by_id(id) do
 Repo.get(MyModel, id)
end

问题是我还没见过其他人做这种类型转换。我担心我没有正确设置Phoenix或Exto。我缺少的Phoenix/Ecto约定是否会自动将我的
Repo.get/2
id参数强制为int?

您的代码是正确的。在即将到来的EXTO版本中,我们希望为这样的情况添加自动铸造。但现在,您需要手动强制转换它。

在Ecto 2.2.0上尝试过,它仍然不会在我的查询中自动将字符串强制转换为整数

where: u.user_id == ^user_id,
出现错误:

(ArgumentError) Postgrex expected an integer in -9223372036854775808..9223372036854775807, got "33133". Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.

从那以后又加了吗?到2020年,建议的模式是什么?