此问题看起来似乎a simple feature,但Phoenix Ecto还没有。在此期间,对于Ecto中的where或查询,有什么解决方法?
例如:
from(u in User,
left_join: up in assoc(u, :user_projects),
# does not work
where: up.project_id != ^project.id OR up.project_id IS NULL,
select: {u.id, u.username})
答案 0 :(得分:6)
我相信您已经误解了or_where
会做什么 - 您可以通过简单的or
和is_nil
完成您的工作:
where: up.project_id != ^project.id or is_nil(up.project_id)
or_where
所做的是,您可以使用where: expr
而不是OR
加入Ecto查询中的多个AND
。在当前版本的Ecto(2.0)中,没有直接的方法可以做到这一点 - Ecto将所有where
表达式与AND
连接起来。
答案 1 :(得分:-1)
只需将OR
替换为and
not
顺便说一下,这是更多的方式