from sqlalchemy import exists
it_exists = Session.query(
exists().where( SomeObject.field==value )
).scalar()
然后会返回True或False。
多选择条件的较复杂判断:
query = session.query(Users).filter(
Users.name.in_( ['Jack', 'Bob', 'Sandy'] ),
Users.age == 18
)
# Below will return True or False
at_least_one_user_exists = session.query(
query.exists()
).scalar()
参考:Calling exists() in sqlalchemy with multiple values in python