]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Copy column that's already attached
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 5 Nov 2020 13:22:18 +0000 (08:22 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 5 Nov 2020 13:22:18 +0000 (08:22 -0500)
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
alembic/operations/toimpl.py
docs/build/unreleased/753.rst [new file with mode: 0644]
tests/test_op.py

index 30edd8492ee80dc60e02174fd79d6df030a6afc4..c6dbafbf1a0fad88a281f0c171fa60f5dc14355e 100644 (file)
@@ -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 (file)
index 0000000..687a851
--- /dev/null
@@ -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.
index acd329ef05a2e5a216eb52014991534d482c5128..7b8dc02766e90992aa65fc04ebe991581fb99a59 100644 (file)
@@ -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(