Elixir, ecto, compare time in where clause

When I create a query using ecto in Elixir, I am not sure how to compare the time in the "where" section.

In the schematic part, I declare create_at as :datetime

  schema "tenant" do field :id, :integer field :created_at, :datetime # timestamps([{:inserted_at,:created_at}]) end 

and the request part is similar to

 def sample_query do query = from t in Tenant, where: t.id == 123, where: t.created_at == %Ecto.DateTime{{2015, 4, 27}, {10, 8, 42}}, select: t end 

It seems that

where: t.created_at <= %Ecto.DateTime{{2015, 4, 27}, {10, 8, 42, 0}},

part of an irregular shape. Can someone tell me how to do it right?

PS: on how to define the create_at field, the link below gave me an answer

Default datetime with Ecto and Elixir

+6
source share
1 answer

You cannot create a %Ecto.DateTime{} structure from an %Ecto.DateTime{} date / time tuple. You need to do:

 def sample_query do query = from t in Tenant, where: t.id == 123, where: t.created_at == ^Ecto.DateTime.from_erl({{2015, 4, 27}, {10, 8, 42, 0}}), select: t end 

If your time values ​​come from somewhere else, and you want to create the %Ecto.DateTime{} structure yourself, you can do:

 ^%Ecto.DateTime{year: 2015, month: 4, day: 27, hour: 10, min: 8, sec: 42, usec: 0} 

(Pay attention to ^)

+7
source

Source: https://habr.com/ru/post/987117/


All Articles