Ecto:Where - 或

时间:2016-11-01 17:33:56

标签: phoenix-framework ecto

此问题看起来似乎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})

2 个答案:

答案 0 :(得分:6)

我相信您已经误解了or_where会做什么 - 您可以通过简单的oris_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

顺便说一下,这是更多的方式