I have 2 tables,let's call them A and B. A has some information about each user, and B has some services of users. Here I want to use SQLAlchemy to query the user and count of their services.
I can use
session.query(A).filter(A.username.like('foo%')) to query users,but I cannot add the sum column.
Best How To :
I'm not sure if there is a more permanent way of attaching the count to the objects but this will let you get the count and the objects at the same time.
from sqlalchemy.sql import func
query = session.query(A, func.count(B.service_name))
query = query.outerjoin(B, A.username == B.username)
query = query.group_by(A.username).filter(A.username.like('foo%'))
for (user, user_service_count) in query.all():