How to make Flask-SQLAlchemy automatically rollback the session if an exception is raised?
you can do something like this:
@app.teardown_request
def teardown_request(exception):
if exception:
db.session.rollback()
db.session.remove()
Have a look here for teardown_request info. You might need to set the PRESERVE_CONTEXT_ON_EXCEPTION
config variable if you are in debug mode.