Postgresql 使用EXTO片段将时间间隔添加到时间戳
我想在phoenix应用程序中使用EXTO片段编写以下查询:Postgresql 使用EXTO片段将时间间隔添加到时间戳,postgresql,elixir,phoenix-framework,Postgresql,Elixir,Phoenix Framework,我想在phoenix应用程序中使用EXTO片段编写以下查询: select * from ( select id, inserted_at + interval '1 day' * expiry as deadline from friend_referral_code ) t where localtimestamp at time zone 'UTC' > deadline 到期日的值是一个表示天数的整数值。到目前为止,我得到的是这样的东西: query = from
select *
from (
select id,
inserted_at + interval '1 day' * expiry as deadline
from friend_referral_code
) t
where localtimestamp at time zone 'UTC' > deadline
到期日的值是一个表示天数的整数值。到目前为止,我得到的是这样的东西:
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)>)
您可以在和之后查询间隔和内部选择
另外,当您将FriendReferralCode作为Repo.all/2调用中的第一个参数传递时,需要将查询作为第一个参数,并将查询作为第二个参数传递,其中需要选项的关键字列表
只需查询|>Repo.all即可。问题是?@AdrianKlaver如何使用fragmentscan用长生不老药书写它请查看我的查询并告诉我哪里错了@AlekseiMatiuskhkinAs您可以从错误消息中看到,这个特定部分的查询是正确构建的。如果您希望得到任何有意义的帮助,请分享代码、完整的错误消息,并指出您遇到困难的部分。我已经编辑了我的问题并添加了长生不老药代码,请查看我更新了答案。啊!!这是一个愚蠢的错误。非常感谢你