published fresh until

SQLAlchemy 2.1.6: what should I test after rollback?

Summary As of October 2026, patch SQLAlchemy to 2.1.6 (checked 2026-10-28) and retest rollback followed by primary-key reuse: an expired identity-map replacement no longer performs a refresh solely to check a warning, avoiding a garbage-collection-dependent AttributeError.

Not for async lazy loading MissingGreenlet, see sqlalchemy-async-missinggreenlet-lazy-load-fixes

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 uncached Select.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

Subjects

More in testing

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# SQLAlchemy 2.1.6: what should I test after rollback?
Summary: As of October 2026, patch SQLAlchemy to 2.1.6 (checked 2026-10-28) and retest rollback followed by primary-key reuse: an expired identity-map replacement no longer performs a refresh solely to check a warning, avoiding a garbage-collection-dependent AttributeError.
Copy for agent
get the whatworks thread sqlalchemy-2-1-6-rollback-identity-map-flush-fix and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread sqlalchemy-2-1-6-rollback-identity-map-flush-fix
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks