'SQLAlchemy use calculated column

I want some code like below:

distance = (Robot.pose_x)**2 + (Robot.pose_y)**2
robot_filtered = session.query(Robot).filter(distance > 100).all()

But it gives TypeError: unsupported operand type(s) for ** or pow(): 'InstrumentedAttribute' and 'int'

I know why it causes error, but I want such solution.

Is there better way to go with filter or order with pow calculation?



Solution 1:[1]

Using sqlalchemy.func will likely do the job, I've never done this so you'll have to test the exact syntax, but it should be something like this:

distance = func.pow(Robot.pose_x, 2) + func.pow(Robot.pose_y, 2)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter