Repository navigation
Add support for SQLAlchemy 2.1 - #834
Merged
Merged
Conversation
SQLAlchemy 2.1 made ScalarAttributeImpl and register_attribute private, breaking import of sqlalchemy_utils. Rather than depending on those internals, reimplement generic_relationship as a hybrid_property with a custom comparator, using only public APIs (object_session, Session.get, Mapper.get_property_by_column, registry.mappers, self_and_descendants). Assignment now goes through regular attribute instrumentation, so re-assigning a generic relationship on a persistent object is flushed. Also pin the test PostgreSQL DSN to psycopg2 since 2.1 changed the default postgresql:// driver to psycopg (v3).
tox installs the latest SQLAlchemy, so once 2.1 is supported every CI job would run against 2.1 only. Add a Python 3.10 job pinned to SQLAlchemy 2.0 so both supported series stay covered. Generated-by: Claude Opus 5
Contributor
Author
|
BTW. I know that #831 PR about it was closed by @josecsotomorales - without explanation why - but in case sqlalchemy-utils maintainers do not want it - the only place we use it is transitively from Flask App Builder - created by my friend @dpgaspar so alternative is that we will PR dpgaspar/Flask-AppBuilder#2484 removing sqlalchemy-utils from FAB. |
Contributor
|
Would be great to get some eyes on this... |
Contributor
Author
|
Indeed. |
Collaborator
josecsotomorales
self-requested a review
October 9, 2026 15:30
Contributor
Author
|
COOOOL ! |
josecsotomorales
approved these changes
Oct 9, 2026
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.
import sqlalchemy_utilsfails on SQLAlchemy 2.1 withAttributeError: module 'sqlalchemy.orm.attributes' has no attribute 'ScalarAttributeImpl', because SQLAlchemy 2.1 madeScalarAttributeImpland related ORM internals private. Sincegeneric.pyis imported from the package's__init__, nothing in sqlalchemy-utils can be imported on 2.1. Fixes #800 and #829. It also affects this repository's own CI, which installs the latest SQLAlchemy.This takes the approach suggested in the review of #804 and #830 (no SQLAlchemy internals): the first commit is @ollz272's #831, unchanged and with its authorship kept, which reimplements
generic_relationshipas a hybrid property using only public APIs (object_session()/Session.get(),registry.mappers,Mapper.get_property_by_column(),self_and_descendants). #831 was closed without a comment; if there was a reason the approach is not acceptable, I'd be glad to know, so it can be addressed here.The second commit keeps SQLAlchemy 2.0 covered: tox installs the latest SQLAlchemy, so it adds a Python 3.10 job pinned to
SQLAlchemy>=2.0,<2.1.Tested locally against SQLite: the full suite gives identical results on SQLAlchemy 2.0.54 and 2.1.1, the same as
masteron 2.0.54 (mastercannot be imported on 2.1.1); allgeneric_relationshiptests pass on both. PostgreSQL, MySQL and MSSQL were not available locally, so those runs are left to CI.Could this be reviewed and released soon? SQLAlchemy-Utils is a transitive dependency of Apache Airflow (through Flask-AppBuilder), and this import error is what blocks Airflow from moving to SQLAlchemy 2.1. Airflow has just had to cap SQLAlchemy below 2.1 to keep CI green (apache/airflow#73822). A release with this fix would let us lift the cap. Thank you for maintaining the library!
Generated-by: Claude Code (Claude Opus 5)