Now that I tried to reproduce this problem I realized that the problem might be that there is also a subquery/1 in use, which means that the example above is more like this instead:
from(p in Post,
left_join: c in assoc(p, :comments),
as: :comment
)
|> subquery()
|> order_by([p, comment: comment], coalesce(comment.title, p.title))
In this case it makes totally sense that binding does not exist and I assume that I have to somehow select coalesce(comment.title, p.title) AS title within subquery and then just order by p.title. Am I right? How can this be done with Ecto?
Thanks for the hint, but I guess for me this would not work since I wanted to reference a subquery binding from outer query which does not make even sense.