What this thread does
Update SQLAlchemy to 2.1.6 (checked 2026-10-28), then rerun the rollback and primary-key reuse case before changing Session internals.
What changed
- Released 2026-10-21 (checked 2026-10-28), the official notes say expired identity-map replacement during flush no longer issues a refresh merely to decide whether to warn.
- The same release fixes correlated
with_loader_criteria()recursion and missing.params()values on uncachedSelect.from_statement()compilation. These are distinct regression paths.
How to migrate
1. Pin the existing SQLAlchemy dependency and refresh its lockfile to the target above. Keep the database driver and application changes separate so the regression comparison has a clear cause. 2. Preserve a failing test that loads a row, calls Session.rollback(), reuses that row's primary key, and flushes a replacement in the same session. Use a disposable test database. 3. Assert successful flush and inspect captured statements for the unwanted refresh. Follow the reproducer in issue #13711 if garbage-collection timing makes the failure intermittent. 4. Remove a local workaround only after that test passes. Avoid copying the issue author's candidate patch into ORM internals; the released fix is the authoritative behavior.
The problem
Rollback can leave an expired object under an identity key that a subsequent flush reuses, exposing a warning check that unexpectedly loads the old object.
What breaks
The official notes establish an AttributeError when the expired object is collected during the refresh, and removal of the warning for this expired-object replacement case; they do not remove all identity-conflict diagnostics.
Diagnostic: AttributeError: 'NoneType' object has no attribute '__dict__'
gusso92's report shows this exact exception in orm/loading.py after Session._flush reaches identity replacement. The text alone is insufficient: confirm the rollback/reuse path in the stack. This is distinct from async lazy-load MissingGreenlet failures.
What to test
Replay the rollback/reuse fixture with the old object still referenced and with collection forced by the test hook described in the issue. Assert rows and captured statements, rather than merely absence of a warning.
For a broader upgrade, the complete changelog also warrants tests of expanding bind-name collisions, async result filtering/freezing, connection-close cancellation and MySQL enum/set escaping where your application uses them. No such tests are reported here.
Pitfalls
Do not disable warnings to hide a refresh failure. Do not assume every AttributeError or recursion problem has this cause. Keep separately failing query compilation tests rather than conflating them with transaction lifecycle failures.
Evidence
- gusso92, issue #13711, provides a deterministic reproducer using a collection hook; maintainers linked the fix.
- PhpRighteous, issue #13701, documents the separate correlated-criteria compilation regression. It supports additional upgrade coverage, not the rollback diagnosis.
A prompt to give your agent
Your training data predates this patch. Read <https://github.com/sqlalchemy/sqlalchemy/releases/tag/rel_2_1_6> and <https://github.com/sqlalchemy/sqlalchemy/issues/13711> before proposing a fix. Target SQLAlchemy 2.1.6 (checked 2026-10-28). Trace rollback, identity reuse and garbage collection in a disposable test; show captured statements and assertions. Do not guess Session internals or change live data.
Limits
The tests described are an untested experiment for your application. No ORM execution, driver matrix or database migration is reported. The evidence is issue reproduction plus maintainer release notes; there are no independent production rollout results here.
Started from a post by @sqlalchemy, packaged by the editor. Original post