使用Ecto片段向时间戳添加间隔 [英] Add interval to timestamp using Ecto Fragments

查看:53
本文介绍了使用Ecto片段向时间戳添加间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用ecto片段在phoenix应用程序中编写以下查询:

I want to write the following query in a phoenix application using Ecto fragments:

select *
from (
  select id, 
  inserted_at + interval '1 day' * expiry as deadline
  from friend_referral_code
) t
where localtimestamp at time zone 'UTC' > deadline

有效期的值是一个整数值,表示天数.到目前为止,我得到的是这样的:

The value of expiry is an integer value that represents number of days. What I've got so far is something like this:

query = from frc in FriendReferralCode,
  where: fragment("localtimestamp at time zone 'UTC'") > fragment("? + INTERVAL '1' * ?", frc.inserted_at, frc.expiry)

FriendReferralCode
|> Repo.all(query)
|> Enum.each(fn frc -> update_friend_referral_code_users(get_friend_referral_code_users_by(receiver_id: frc.id), %{status: false}) end)
|> IO.puts()

结束

但它会引发以下错误:

** (EXIT) an exception was raised:
        ** (FunctionClauseError) no function clause matching in Keyword.merge/2
            (elixir 1.11.2) lib/keyword.ex:764: Keyword.merge([], #Ecto.Query<from f0 in Stakester.Accounts.FriendReferralCode, where: fragment("localtimestamp at time zone 'UTC'") > fragment("? + INTERVAL '1 day' * ?", f0.inserted_at, f0.expiry)>)

推荐答案

您在此外, Repo.all/2 将查询作为第一个参数,而在调用 Repo.all/2 的过程中将 FriendReferralCode 作为第一个参数传递时,则期望查询,然后将 query 作为第二个查询,它需要关键字的选项列表.

Also, Repo.all/2 expects a query as a first argument, while you pass FriendReferralCode as the first argument in the call to Repo.all/2, where it expects a query, and query as a second one, where it expects a keyword list of options.

只执行 query |>改为Repo.all().

这篇关于使用Ecto片段向时间戳添加间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆