Skip to content

Add support for SQLAlchemy 2.1 - #830

Closed
ollz272 wants to merge 1 commit into
kvesteri:masterfrom
ollz272:sqlalchemy-2.1-support
Closed

ollz272 wants to merge 1 commit into
kvesteri:masterfrom
ollz272:sqlalchemy-2.1-support

Conversation

@ollz272

@ollz272 ollz272 commented Sep 25, 2026 •

Copy link
Copy Markdown

Summary

import sqlalchemy_utils fails on SQLAlchemy 2.1:

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

SQLAlchemy 2.1 made several attribute internals private. This PR:

  • Imports _ScalarAttributeImpl / _register_attribute on 2.1+, falling back to ScalarAttributeImpl / register_attribute on 2.0 (used by generic_relationship).
  • Makes the version check in GenericAttributeImpl tolerate pre-release version strings (e.g. 2.1.0b1), which previously raised ValueError.
  • Pins the test PostgreSQL DSN to postgresql+psycopg2://. SQLAlchemy 2.1 changed the default postgresql:// driver to psycopg (v3), which CompositeType doesn't support; this keeps the tests using the same driver as before.

Testing

Full suite run locally against PostgreSQL and MySQL on both SQLAlchemy 2.1.0 and 2.0.54: 2690 passed on each, with identical results. The one failure on both is the psycopg2cffi test, because that driver isn't installed locally. MSSQL was not tested locally.

Notes

  • CompositeType still only supports psycopg2. On SQLAlchemy 2.1, users with a plain postgresql:// URL now get psycopg3 and a TypeError about cursor_factory. They need postgresql+psycopg2:// for now. psycopg3 support could come in a follow-up.
  • tox installs the latest SQLAlchemy, so CI will now test 2.1 only. It may be worth adding a 2.0 job to the matrix.

SQLAlchemy 2.1 made ScalarAttributeImpl and register_attribute private
(_ScalarAttributeImpl, _register_attribute), breaking import of
sqlalchemy_utils. Import the new names with a fallback for 2.0.

Also make the version check tolerate pre-release versions (e.g.
2.1.0b1), and pin the test PostgreSQL DSN to psycopg2 since 2.1 changed
the default postgresql:// driver to psycopg (v3).
@kurtmckee

Copy link
Copy Markdown
Collaborator

sqlalchemy is explicitly discouraging this usage. Changing sqlalchemy-utils to support this usage doesn't align with what upstream is explicitly trying to accomplish.

I'm open to a solution that doesn't rely on sqlalchemy internals like this, though!

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.

2 participants