sqlalchemy - 在单个表中,Flask Sqlalchemy 如何删除所有行

如何使用Flask-SQLAlchemy删除单个表中的所有行?


>>> users = models.User.query.all()
>>> models.db.session.delete(users)

# but it errs out: UnmappedInstanceError: Class '__builtin__.list' is not mapped

时间:

试用delete


models.User.query.delete()

来自文档Returns the number of rows deleted, excluding any cascades.


num_rows_deleted = db.session.query(Model).delete()

另外,不要忘记,在你提交之前,删除不会生效,如下面的片段所示:


try:
 num_rows_deleted = db.session.query(Model).delete()
 db.session.commit()
except:
 db.session.rollback()

...