1 year ago

#382014

test-img

Abel B.

DetachedInstanceError Instance User is not bound to a Session - SQLAlchemy

I am trying to get a window of my web and it gives me the following error, it seems to be due to the database session cleanup:

File "E:\app\templates\menu_template.html", line 56, in block 'content'
<span class="text-dark">{{ current_user.nombre }}</span>
File "c:\python\python39\lib\site-packages\jinja2\environment.py", line 474, in getattr
return getattr(obj, attribute)
File "c:\python\python39\lib\site-packages\sqlalchemy\orm\attributes.py", line 480, in __get__
return self.impl.get(state, dict_)
File "c:\python\python39\lib\site-packages\sqlalchemy\orm\attributes.py", line 926, in get
value = state._load_expired(state, passive)
File "c:\python\python39\lib\site-packages\sqlalchemy\orm\state.py", line 675, in _load_expired
self.manager.expired_attribute_loader(self, toload, passive)
File "c:\python\python39\lib\site-packages\sqlalchemy\orm\loading.py", line 1343, in load_scalar_attributes
raise orm_exc.DetachedInstanceError(
sqlalchemy.orm.exc.DetachedInstanceError: Instance <User at 0x2871d0746d0> is not bound to a 
Session; attribute refresh operation cannot proceed (Background on this error at: 
http://sqlalche.me/e/14/bhk3)

I am doing the cleaning of the database sessions in the following way:

def register_error_handlers(app):
@app.errorhandler(500)
def base_error_handler(e):
    return render_template('500.html'), 500

@app.errorhandler(404)
def error_404_handler(e):
    return render_template('404.html'), 404

@app.errorhandler(401)
def error_404_handler(e):
    return render_template('401.html'), 401

@app.teardown_appcontext
def teardown_db(e):
    db.session.close_all()
    db.engine.dispose()

What am i missing here?

python

flask

sqlalchemy

flask-sqlalchemy

0 Answers

Your Answer

Accepted video resources