Skip to content Skip to sidebar Skip to footer

How To Use A Labelled Column In Sqlalchemy Filter?

I want to use the labelled column in sqlalchemy filter. for eg: db.session.query( PartMaster.name, PartMaster.description, PartTracker.actual_length, func.sum(PartT

Solution 1:

In SQL if you want to filter rows by result, you need to use HAVING instructions.

So in your case:

db.session.query(
    PartMaster.name,
    PartMaster.description,
    PartTracker.actual_length,
    func.sum(PartTracker.quantity).label('quantity')
).join(PartTracker).group_by(
    PartTracker.part_master_id,PartTracker.actual_length
).having(
    func.sum(PartTracker.quantity) > 0
)

Example from doc

Post a Comment for "How To Use A Labelled Column In Sqlalchemy Filter?"