schema = operation.schema
kw = operation.kw
+ if column.table is not None:
+ column = column.copy()
+
t = operations.schema_obj.table(table_name, column, schema=schema)
operations.impl.add_column(table_name, column, schema=schema, **kw)
--- /dev/null
+.. change::
+ :tags: bug, operations
+ :tickets: 753
+
+ Modified the ``add_column()`` operation such that the ``Column`` object in
+ use is shallow copied to a new instance if that ``Column`` is already
+ attached to a ``table()`` or ``Table``. This accommodates for the change
+ made in SQLAlchemy issue #5618 which prohibits a ``Column`` from being
+ associated with multiple ``table()`` objects. This resumes support for
+ using a ``Column`` inside of an Alembic operation that already refers to a
+ parent ``table()`` or ``Table`` as well as allows operation objects just
+ autogenerated to work.
from sqlalchemy import exc
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
+from sqlalchemy import MetaData
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy.sql import column
op.add_column("t1", Column("c1", Integer, nullable=False))
context.assert_("ALTER TABLE t1 ADD COLUMN c1 INTEGER NOT NULL")
+ def test_add_column_already_attached(self):
+ context = op_fixture()
+ c1 = Column("c1", Integer, nullable=False)
+ Table("t", MetaData(), c1)
+
+ op.add_column("t1", c1)
+ context.assert_("ALTER TABLE t1 ADD COLUMN c1 INTEGER NOT NULL")
+
def test_add_column_w_check(self):
context = op_fixture()
op.add_column(