flask-sqlalchemy
flask-sqlalchemy copied to clipboard
Adopt new context tracking sysyem
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 ong
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
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
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.
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)
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.
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.
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?
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.
Quart 0.18.3 should fix the app_ctx
issue.