SQLAlchemy exists 判断某条数据是否存在

参考:https://stackoverflow.com/questions/6587879/how-to-elegantly-check-the-existence-of-an-object-instance-variable-and-simultan

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

猜你喜欢

转载自blog.csdn.net/weixin_33738555/article/details/86943471