Skip to content

Add support for SQLAlchemy 2.1 - #834

Merged
josecsotomorales merged 3 commits into
kvesteri:masterfrom
potiuk:sqlalchemy-2.1-support
Oct 9, 2026
Merged

josecsotomorales merged 3 commits into
kvesteri:masterfrom
potiuk:sqlalchemy-2.1-support

Conversation

@potiuk

@potiuk potiuk commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

import sqlalchemy_utils fails on SQLAlchemy 2.1 with AttributeError: module 'sqlalchemy.orm.attributes' has no attribute 'ScalarAttributeImpl', because SQLAlchemy 2.1 made ScalarAttributeImpl and related ORM internals private. Since generic.py is 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_relationship as 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 master on 2.0.54 (master cannot be imported on 2.1.1); all generic_relationship tests 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)

ollz272 and others added 2 commits October 6, 2026 01:43
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
@potiuk

potiuk commented Oct 6, 2026

Copy link
Copy Markdown
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 forego sqlalchemy-utils as dependency.

PR dpgaspar/Flask-AppBuilder#2484 removing sqlalchemy-utils from FAB.

@ollz272

ollz272 commented Oct 8, 2026

Copy link
Copy Markdown
Contributor

Would be great to get some eyes on this...

@potiuk

potiuk commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

Indeed.

@josecsotomorales

josecsotomorales commented Oct 9, 2026 •

Copy link
Copy Markdown
Collaborator

@potiuk @ollz272, really sorry for that. I didn't mean to close that PR. It looks like an automated workflow from an internal project auto-closed this issue. That said, I'm going to get it merged and will drop a new release with SA 2.1 support 🚀

@josecsotomorales
josecsotomorales self-requested a review October 9, 2026 15:30
@josecsotomorales
josecsotomorales merged commit 2b9cb9d into kvesteri:master Oct 9, 2026
11 checks passed
@potiuk

potiuk commented Oct 9, 2026

Copy link
Copy Markdown
Contributor Author

COOOOL !

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AttributeError: module 'sqlalchemy.orm.attributes' has no attribute 'ScalarAttributeImpl'

3 participants