【问题标题】:Querying by DateTime in Ecto在 Ecto 中按日期时间查询
【发布时间】:2015-05-14 19:28:20
【问题描述】:

这是我尝试过的。

date = Ecto.DateTime.from_erl(:calendar.universal_time())
query |> where([record], record.deadline >= ^date)

我也试过

date = Ecto.DateTime.from_erl(:calendar.universal_time())
query = from m in MyApp.SomeModel,
      where: m.deadline >= ^date,
      select: m

两者都返回相同的消息

value `%Ecto.DateTime{..}` in `where` cannot be cast to type :datetime in query

据我了解,我应该在查询中使用 Ecto.DateTime。也许我错过了一些明显的东西?谢谢!

【问题讨论】:

    标签: elixir phoenix-framework ecto


    【解决方案1】:

    :datetime 类型是本机类型,仅适用于元组。如果您在架构中将列类型设置为 Ecto.DateTime,它将能够使用更高级别的类型,例如 Ecto.DateTime 结构。

    【讨论】:

    • 太棒了!谢谢。工作得很好。更改:field :deadline, :datetime 至:field :deadline, Ecto.DateTime
    猜你喜欢
    • 1970-01-01
    • 2017-05-25
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多