Repository navigation
fix(sqla): recognise flask-sqlalchemy-lite when its first import fails - #2998
Closed
vishalkaay wants to merge 1 commit into
Closed
vishalkaay wants to merge 1 commit into
vishalkaay wants to merge 1 commit into
Conversation
SQLAlchemy 2.1 no longer installs greenlet by default. Without it, importing Flask-SQLAlchemy-Lite fails the first time in a process and succeeds the second time. Flask-Admin's optional import absorbed that failure and treated the package as not installed, the application's own import then worked, and its db object was no longer recognised: every list view failed with `AttributeError: 'SQLAlchemy' object has no attribute 'query'`. Fall back to the module the application loaded when checking for the extension class, so the db object is recognised whichever import ran first.
vishalkaay
force-pushed
the
fix/sqla-lite-broken-import
branch
from
November 10, 2026 20:07
6d7d9d2 to
25f3126
Compare
Collaborator
|
Isn't this a bug in flask-sqlalchemy-lite or even asyncio/(greenlet more than in flask-admin? |
Collaborator
|
flask-sqla-lite even declares "sqlalchemy[asyncio]>=2.0.31", as a dependency, so greenlet should be installed when flask-sqlalite is present |
Collaborator
|
even |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Every SQLAlchemy list view fails with
AttributeError: 'SQLAlchemy' object has no attribute 'query'when Flask-SQLAlchemy-Lite is used with SQLAlchemy 2.1 andgreenletis not installed (#2994).Cause. SQLAlchemy 2.1 no longer installs
greenletby default.flask_sqlalchemy_liteimportssqlalchemy.ext.asyncio, which raisesImportErrorwithout it. That import fails only the first time in a process; a second attempt succeeds:flask_admin/contrib/sqla/_types.pycatches theImportErrorand setsT_SQLALCHEMY_LITE = None. So whenflask_adminis imported before the application importsflask_sqlalchemy_lite, Flask-Admin absorbs the one failure, the application's import works, and_is_db_extension(db)is false for the application's db object.get_query()then calls.queryon the extension instead of its session. SQLAlchemy 2.0 is unaffected because it depends on greenlet.Change.
_is_db_extension()now falls back to theflask_sqlalchemy_litemodule insys.moduleswhen Flask-Admin's own import failed. An application can only hold a Flask-SQLAlchemy-Lite db object if that module is loaded, so the object is recognised whichever import ran first. Nothing changes when the import in_typessucceeds, or when the package cannot be imported at all (e.g. installed next to SQLAlchemy 1.4).The first version of this PR re-raised the import error instead. That broke
py311-min, where Flask-SQLAlchemy-Lite is installed alongside SQLAlchemy 1.4 and can never import, so I replaced it with this approach.The new test simulates the failed import and checks that a view created with a Flask-SQLAlchemy-Lite db object still serves its list view. It fails on master. I also checked the real case by hand: SQLAlchemy 2.1.1 without greenlet now returns 200 for the list view.
fixes #2994