python - SQLAlchemy: filter by relationship -
i have 2 tables, user , object, have one-to-many relationship (a user can have many objects ).
how can filter users have @ least 1 object, in pep8 compliant way?
this code works, not pep8-compliant:
query = session.query(user.id) query = query.filter(user.objects != none) the documentation mentions using isnot: http://docs.sqlalchemy.org/en/rel_1_0/orm/tutorial.html#common-filter-operators
but following code gives rise not implemented error.
query = session.query(user.id) query = query.filter(user.objects.isnot(none))
as pointed out, isnot not implemented relationships, simple columns.
as relationships, there general more powerful construct any(criterion, ...).
in case can write pep8-compliant code below, produce same sql in question:
q = session.query(user.id) q = q.filter(user.objects.any()) but allows more complicated queries, like: return users, not have objects value > 100:
q = session.query(user.id) q = q.filter(~user.objects.any(object.value > 100))
Comments
Post a Comment