数据库
首页 > 数据库> > python-在SQLAlchemy中调用delete()后行仍然存在

python-在SQLAlchemy中调用delete()后行仍然存在

作者:互联网

我想拥有一条Flask路由,该路由删除SQLAlchemy模型VisitLog的所有实例.我调用VisitLog.query.delete(),然后重定向回页面,但旧条目仍然存在.没有错误.他们为什么不删除?

@app.route('/log')
def log():
    final_list = VisitLog.query.all()
    return render_template('log.html', loging=final_list)

@app.route('/logclear')
def logclear():
    VisitLog.query.delete()
    return redirect("log.html", code=302)
<a href="{{ url_for('logclear') }}">Clear database</a>

解决方法:

与其他写入操作一样,您必须在执行批量删除后提交会话.

VisitLog.query.delete()
db.session.commit()

标签:sqlalchemy,flask,flask-sqlalchemy,python
来源: https://codeday.me/bug/20191118/2026805.html