无法强制转换为类型:查询中的naive_datetime

use*_*168 5 elixir ecto phoenix-framework

我有疑问:

 PhoenixApp.one(from r in PhoenixApp.Reply,
              where: r.inserted_at > ^datatime,
              select: count(r.id))
Run Code Online (Sandbox Code Playgroud)

此查询失败,并显示错误: value '#Ecto.DateTime<2016-12-04 20:11:21>' in 'where' cannot be cast to type :naive_datetime in query:

但是当使用type(^datatime, Ecto.DateTime)它的转换数据时工作.

问题:它发生了什么,看起来像datatime最初有DateTime类型?

使用新的Ecto 2.1.2

Dog*_*ert 8

看起来Ecto不支持从弃用Ecto.DateTime到新Elixir NaiveDateTime结构的转换,当您:naive_datetime在Ecto 2.1+中使用时,它正在使用它:

iex(1)> dt = Ecto.DateTime.utc
#Ecto.DateTime<2017-01-08 12:11:59>
iex(2)> Ecto.Type.cast :naive_datetime, dt
:error
Run Code Online (Sandbox Code Playgroud)

Ecto 2.1中推荐的方法是停止使用弃用的Ecto.DateTimestruct并在任何地方使用Elixir的新NaiveDateTime(或DateTime).如果您仍想在它们之间进行显式转换,则可以|> Ecto.DateTime.to_erl |> NaiveDateTime.from_erl!在查询中执行并使用该值:

iex(1)> dt = Ecto.DateTime.utc
#Ecto.DateTime<2017-01-08 12:13:50>
iex(2)> dt |> Ecto.DateTime.to_erl |> NaiveDateTime.from_erl!
~N[2017-01-08 12:13:50]
Run Code Online (Sandbox Code Playgroud)

如果您从Ecto查询中获取旧结构,则可能在模式中为它们定义了旧类型,您应该将其更改field :foo, Ecto.DateTimefield :foo, NaiveDateTime(或field :foo, DateTime).

另见:https://github.com/elixir-ecto/ecto/blob/v2.1/CHANGELOG.md#integration-with-elixir-13-calendar-types.