I found out that SQLAlchemy translates
db.query(...).filter(A.id.in_(ids))
into
SELECT ... FROM a WHERE a.id != a.id
use subquery, if ids is empty, will (never executed).
ids
example:
subquery = db.query(SomeTable.id).filter(...).subquery() db.query(...).filter(A.id.in_(subquery))
into:
SELECT ... FROM a WHERE a.id IN (SELECT ...)