Landed upstreamCorrectnessLanded Aug 10, 2026
Copy column defaults in Table.to_metadata
Fixed SQLAlchemy Table.to_metadata() so copied tables retain independent default, on-update, server-default, and sequence ownership instead of mutating the source table's default objects.
sqlalchemy/sqlalchemy · #13482
Problem
Table.to_metadata() reused the original column's default, onupdate, server_default, and server_onupdate objects. Attaching those objects to the copied column changed their column reference, leaving the original column's defaults associated with the copy.
Approach
Copied each default generator before constructing the new column and copied Sequence defaults into the target metadata collection so original and cloned tables keep separate default objects.
Impact and scope
- Protects SQLAlchemy schema-copy correctness for applications that clone metadata before migration, reflection, testing, or multi-tenant table generation.
- Prevents subtle ownership corruption where copied columns can silently steal default generator references from source columns.
- Landed through SQLAlchemy's Gerrit workflow as an authored upstream commit while preserving the GitHub PR and issue closure trail.
Validation
- Added regression coverage for client-side defaults, server-side defaults, on-update values, and sequence defaults.
- Updated merge-copy expectations to assert that each copied default remains associated with its own column.
- Final upstream commit closes GH-13481 and PR #13482; the rel_2_0 backport remains separate and is not counted here.