Flask-admin: SQLAlchemy: The data is never refreshed

Created on 28 Apr 2016  路  6Comments  路  Source: flask-admin/flask-admin

The problem:
The data is never refreshed in the UI when modified by something else than the UI.

Steps to reproduce:

  • We set up a flask admin with a SQLAlchemy session
  • Load the view, see the data
  • Modify the database using any other source (eg. change a value using psql)
  • Refresh the page and see the data stays the same

Proposed solution is to expunge the objects after we load them to the session, or open/close the session every time we refresh the page although its open for discussion.

Most helpful comment

I got similar problems but after changing data from admin panel (especially for columns with relationships).
You could see it (after changes was made in the view) in following way:

  • reload screen with list_view few times: old data and then changed data will be shown randomly
  • or the same behavior was for reloading screen with edit_form

Technologies used for my case: SqlAlchemy (not Flask version), Gunicorn with few workers and Nginx
Also, it could not be reproduced locally, only on the server with production environment

So the solution for my case was to add:

@app.teardown_appcontext  # the same as @app.teardown_request but for Flask >=0.9
def shutdown_session(exception=None):
    # load all expired attributes for the given instance
    session.expire_all()

If I'm missing smth, please let me know
And here you could find some useful info

All 6 comments

If you change data with psql - do you commit it? What is your transaction isolation level? Do you use scoped session (and close it at the end of each request as suggested by SQLAlchemy docs)?

Also, see if this might help you: https://github.com/flask-admin/flask-admin/issues/1166

Thank you for the prompt response @mrjoes.

When I edit the data, I commit the session. I do use scoped session with flask-admin, I have tried to use the isolation_level='READ COMMITTED' as suggested by @busturdust in #1166 but unfortunately it did not help.

Do you close scoped session after each request?

http://docs.sqlalchemy.org/en/latest/orm/contextual.html#sqlalchemy.orm.scoping.scoped_session.remove

This is required and Flask-SQLAlchemy automatically does it. If you use vanilla SQLAlchemy, you will have to do it yourself. Failing to close the session will keep all tracked models in the memory and will cause the memory leaks.

Here's how it should look like:

@app.teardown_request
def app_teardown(response_or_exc):
    # Assuming that `session` is your scoped session 
    session.remove()
    return response_or_exc

Also see here: http://flask.pocoo.org/docs/0.10/patterns/sqlalchemy/

Although I'm not using Flask sqlalchemy, it seems removing the session in the @app.teardown_request does help.

Thank you so much for your help @mrjoes

I got similar problems but after changing data from admin panel (especially for columns with relationships).
You could see it (after changes was made in the view) in following way:

  • reload screen with list_view few times: old data and then changed data will be shown randomly
  • or the same behavior was for reloading screen with edit_form

Technologies used for my case: SqlAlchemy (not Flask version), Gunicorn with few workers and Nginx
Also, it could not be reproduced locally, only on the server with production environment

So the solution for my case was to add:

@app.teardown_appcontext  # the same as @app.teardown_request but for Flask >=0.9
def shutdown_session(exception=None):
    # load all expired attributes for the given instance
    session.expire_all()

If I'm missing smth, please let me know
And here you could find some useful info

@app.teardown_request
def app_teardown(response_or_exc):
    # Assuming that `session` is your scoped session 
    session.remove()
    return response_or_exc

This helped @mrjoes. I was having inconsistent results in my Flask-Admin. After I moved my code to production.

Was this page helpful?
0 / 5 - 0 ratings