Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ecto: Where - Or

It looks like a simple feature is coming for this issue, but Phoenix Ecto doesn't have it yet. What is a workaround in the meantime for a where-or query in Ecto?

For example:

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})
like image 666
steel Avatar asked Nov 01 '16 17:11

steel


1 Answers

I believe you've misunderstood what or_where will do -- what you want can be done with a simple or and is_nil:

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

What or_where does is enable you to join multiple where: expr in an Ecto query with an OR instead of AND. In the current version of Ecto (2.0), there is no straightforward way to do that -- Ecto joins all where expressions with an AND.

like image 115
Dogbert Avatar answered Oct 30 '22 21:10

Dogbert