Ecto:哪里 - 或

Ecto: Where - Or

好像a simple feature is coming for this issue, but Phoenix Ecto还没有。同时在 Ecto 中进行 where-or 查询的解决方法是什么?

例如:

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})

只需将 OR 替换为 andnot

from(u in User,
left_join: up in assoc(u, :user_projects),
where: up.project_id != ^project.id AND not(is_nil(up.project_id)),
select: {u.id, u.username})

顺便说一句,这是更外向的方式

我相信你误解了 or_where 会做什么 -- 你想要的可以用简单的 oris_nil:

来完成
where: up.project_id != ^project.id or is_nil(up.project_id)

or_where 的作用是使您能够使用 OR 而不是 AND 在 Ecto 查询中加入多个 where: expr。在当前版本的 Ecto (2.0) 中,没有直接的方法可以做到这一点——Ecto 将所有 where 表达式与 AND.

连接起来