Ecto Query - Dates + Postgres Intervals + Query Interpolation

送分小仙女□ 提交于 2019-12-03 21:15:41

I needed to do exactly this a while ago and ended up using the fact that you can multiply intervals with $1.

postgres=# select interval '1 year' - interval '1 month' * 5;
 ?column?
----------
 7 mons
(1 row)

So, this should work:

query = from ch in Child, 
            join: loc in assoc(ch, :location),
            where: ch.birthday <= fragment("(now() AT TIME ZONE ?)::date - interval '1 month' * ?", loc.time_zone, 2)

Repo.all(query)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!