-->

Querying by DateTime in Ecto

2019-06-16 10:46发布

问题:

Here is what I have tried.

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

I also tried

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

Both return same message

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

From what I understand I am supposed to be using Ecto.DateTime in my queries. Maybe I am missing something obvious? Thanks!

回答1:

The :datetime type is a native type and works only with tuples. If you set your column type to Ecto.DateTime in your schema, it will be able to work with higher level types like the Ecto.DateTime struct.