from sqlalchemy import func


class BaseQuery(Query):

    def count_star(self):
        count_query = self.statement.with_only_columns([func.count()]
            ).order_by(None)
return self.session.execute(count_query).scalar()
How to count rows with SELECT COUNT(*) with SQLAlchemy?