SQLALchemy: TypeError: bad operand type for abs(): 'BinaryExpression'

Viewed 13

I am trying to convert the SQL command select * from chart where abs(jupiter_lon-h5_lon) < 0.88 into SQLAlchemy:

db.session.query(Chart).filter(abs(Chart.jupiter_lon - Chart.h5_lon) < 0.88).all()

but I'm getting the error: TypeError: bad operand type for abs(): 'BinaryExpression' - which I don't understand. What's wrong here?

1 Answers

You cannot use arbitrary Python functions in SQLAlchemy filters, because the filter expression is translated to SQL in the end. They must be SQLAlchemy filter compatible functions and imported through the SQLAlchemy package.

Related