From: Mike Bayer Date: Thu, 5 Nov 2020 13:22:18 +0000 (-0500) Subject: Copy column that's already attached X-Git-Tag: rel_1_5_0~17^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a41d5e604fbea1e4c1723db9b826b394c702e34b;p=thirdparty%2Fsqlalchemy%2Falembic.git Copy column that's already attached 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. Change-Id: Idc6933cf0da40373f4f270d75883995822c072f0 Fixes: #753 --- diff --git a/alembic/operations/toimpl.py b/alembic/operations/toimpl.py index 30edd849..c6dbafbf 100644 --- a/alembic/operations/toimpl.py +++ b/alembic/operations/toimpl.py @@ -127,6 +127,9 @@ def add_column(operations, operation): 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) diff --git a/docs/build/unreleased/753.rst b/docs/build/unreleased/753.rst new file mode 100644 index 00000000..687a8518 --- /dev/null +++ b/docs/build/unreleased/753.rst @@ -0,0 +1,12 @@ +.. 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. diff --git a/tests/test_op.py b/tests/test_op.py index acd329ef..7b8dc027 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -7,6 +7,7 @@ from sqlalchemy import event 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 @@ -111,6 +112,14 @@ class OpTest(TestBase): 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(