flask-sqlalchemy icon indicating copy to clipboard operation
flask-sqlalchemy copied to clipboard

Adopt new context tracking sysyem

Open guillermohs9 opened this issue 2 years ago • 1 comments

Hi! I'm using this extension is a couple of Flask projects and a Quart one. When trying to migrate the Quart app to it's latest version, the extension stopped working with the flask_patch:

ImportError: cannot import name '_app_ctx_stack' from 'quart.flask_patch'

According to what I understand from this answer, _app_ctx_stack will soon no longer be available in Flask either.

According to Quart's changelog:

Remove usage of LocalStacks, using ContextVars more directly. This should improve performance, but introduces backwards incompatibility. _*_ctx_stack globals are removed, use *_context instead. Extensions should store on g as appropriate.

Are there any plans on making the appropiate changes on this extension to support this newer Quart release and Flask's future releases that drop _app_ctx_stack?

Thanks

guillermohs9 avatar Jul 25 '22 13:07 guillermohs9

Accessing the context will now also lead to a deprecation warning

venv/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py:14: in <module>
    from flask import _app_ctx_stack, abort, current_app, request
venv/lib/python3.7/site-packages/flask/__init__.py:56: in __getattr__
    stacklevel=2,
E   DeprecationWarning: '_app_ctx_stack' is deprecated and will be removed in Flask 2.3

Same applies to _request_ctx_stack

kasium avatar Aug 03 '22 11:08 kasium

Fixed in #1087. Using db.session or db.engine now always requires an active Flask application context. The session is scoped to the current context, which ensures it is always cleaned up.

davidism avatar Sep 18 '22 16:09 davidism

Still doesn't seem to be working with Quart using the latest 3.0.0a2 version.

Traceback (most recent call last):
  File "c:\Code\proj\run.py", line 14, in <module>
    from flask_sqlalchemy import SQLAlchemy
  File "c:\Code\proj\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 5, in <module>
    from .extension import SQLAlchemy
  File "c:\Code\proj\env\lib\site-packages\flask_sqlalchemy\extension.py", line 23, in <module>
    from .session import _app_ctx_id
  File "c:\Code\proj\env\lib\site-packages\flask_sqlalchemy\session.py", line 8, in <module>
    from flask.globals import app_ctx
ImportError: cannot import name 'app_ctx' from 'quart.flask_patch.globals' (c:\Code\proj\env\lib\site-packages\quart\flask_patch\globals.py)

pdjohntony avatar Oct 02 '22 16:10 pdjohntony

Check the traceback, that's an issue with Quart (which I think is also fixed). Regardless, this is Flask-SQLAlchemy, we don't provide direct compatibility with Quart.

davidism avatar Oct 02 '22 17:10 davidism

This is also the latest version of Quart so its not fixed quite yet, and looks like it might not get fixed at all based on this comment https://github.com/pallets/quart/issues/156#issuecomment-1179691917 about flask-patching possibly being removed from Quart in the future.

Regardless, I understand this is a Quart issue and not a Flask-SQLAlchemy issue. I just wanted to share that this isn't working with Quart currently. Thanks.

pdjohntony avatar Oct 02 '22 18:10 pdjohntony

Chech the traceback, that's an issue with Quart (which I think is also fixed). Regardless, this is Flask-SQLAlchemy, we don't provide direct compatibility with Quart.

Although I understand direct compatibility with Quart isn't provided, wouldn't importing app_ctx be incompatible when Flask 2.3 is out?

guillermohs9 avatar Oct 05 '22 11:10 guillermohs9

The opposite, app_ctx is what's compatible with Flask >= 2.2. Please open a new issue with the appropriate project if you have further issues.

davidism avatar Oct 05 '22 13:10 davidism

Quart 0.18.3 should fix the app_ctx issue.

pgjones avatar Oct 08 '22 09:10 pgjones