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})
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With