日期范围的 Ecto 查询

Ecto query for date range

我必须根据模型中的时间戳查询日期范围。 到目前为止我尝试过的是

str="2017-12-18"
{:ok, ed}=Ecto.Date.cast(str)
ed= ed |> Ecto.Date.from_erl  |> NaiveDate.from_erl!
Repo.all(from l in Log, where: l.inserted_at == ^ed)

但是postgres中的日期是日期时间的形式,例如2017-12-18 19:58:01.414.

但我只想查询日期而不是时间。但是,如果我使用 Ecto.DateTimeNaiveDateTime 并在字符串中传递准确的日期时间,它就可以工作。但我只想匹配日期。这怎么可能? 谢谢

您可以使用 fragment 将字段转换为日期,然后进行比较:

Repo.all(from l in Log, where: fragment("?::date", l.inserted_at) == ^ed)