Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Querying by DateTime in Ecto

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!

like image 301
JB. Avatar asked May 14 '15 19:05

JB.


Video Answer


1 Answers

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.

like image 64
José Valim Avatar answered Sep 21 '22 02:09

José Valim