Prøv en af de to versioner nedenfor (men ikke sikker på, at den anden fungerer på MySQL):
rstuff = (Foo.max_stuff - db.func.sum(Bar.stuff))
qry = (
db.session.query(
Foo.id,
Foo.name,
rstuff.label('rstuff')
)
.join(Bar)
.group_by(Foo.id)
# version-1: probably universal, but repeats the expression
.having(rstuff >= 3)
# version-2: might depend on the RMDBS engine
# .having(db.literal_column('rstuff') >= 3)
)