Using a label in a having clause in sqlachemy

前端 未结 1 1870
醉梦人生
醉梦人生 2021-01-26 20:49

I\'m trying to use a label in a having clause with sqlalchemy, but I\'m having problems getting it working. I\'m trying something like this:

qry = db.session.que         


        
相关标签:
1条回答
  • 2021-01-26 21:24

    Try any of the two versions below (not sure the second one works on MySQL though):

    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)
    )
    
    0 讨论(0)
提交回复
热议问题