I have a raw inner join query with counting, written directly on Postgres SQL:
SELECT \"films\".\"id\" AS \"megaId\",
COUNT(\"filmComments\".
Mapping that to SQLAlchemy should be quite straightforward. I'm not considering the aliases, for obvious reasons.
from sqlalchemy import func
megaId, numOfComments = (session.query(Film.id, func.count(FilmComment.id))
.join(FilmComment, Film.id == FilmComment.filmId)
.group_by(Film.id).first())
This should work. The explicit on
clause wouldn't be needed if FilmComment.filmId
were declared as a foreign key.