Using relationship
s (small and simple code, but a bit more verbose SQL
with EXISTS
and not really required JOIN on groups
table):
query = session.query(Users).filter(~Users.groups.any())
Otherwise, using outerjoin
with where
, which is very direct and generates SQL
resembling one-to-one the query:
query = (session.query(Users)
.outerjoin(group_table, group_table.c.user_id == Users.id)
.filter(group_table.c.user_id == None)
)