SQLAlchemy: filter by relationship
As you pointed out, isnot
is not implemented for relationships, but only for simple columns.
As for relationships, there is a general even more powerful construct any(criterion, ...)
.
In your case you can write the PEP8-compliant code below, which will produce exactly the same SQL
as in your question:
q = session.query(User.id)
q = q.filter(User.objects.any())
But it also allows you to do more complicated queries, like: return Users, which do not have objects with value > 100
:
q = session.query(User.id)
q = q.filter(~User.objects.any(Object.value > 100))