Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doesn't correctly close flask-sqlachemy-lite transactions/sessions #2585

Open
ddorian opened this issue Dec 25, 2024 · 1 comment
Open

Doesn't correctly close flask-sqlachemy-lite transactions/sessions #2585

ddorian opened this issue Dec 25, 2024 · 1 comment

Comments

@ddorian
Copy link

ddorian commented Dec 25, 2024

Hi,

Clone this minimal repo and follow the readme to reproduce https://github.com/ddorian/admin-sqlalite-bug

You will get the following traceback
full_traceback.txt

Environment: it's all in the repo.

  • Python version: 3.10
  • Flask version: 3.1.0
  • Flask-Admin version: a4e7be8

Regards

@ddorian
Copy link
Author

ddorian commented Dec 25, 2024

An alternative would be to pass a scoped session to the ModelView. Seems to work fine for now.

def get_scoped_session(db: SQLAlchemy, **options):
    """Scoped session for flask-admin.
    See https://github.com/pallets-eco/flask-admin/issues/2585
    """

    def _app_ctx_id() -> int:
        """Get the id of the current Flask application context for the session scope."""
        return id(app_ctx._get_current_object())  # type: ignore[attr-defined]

    scope = options.pop("scopefunc", _app_ctx_id)
    return scoped_session(db.sessionmaker, scope)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant