]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Fix duplicated constraints when using expressions
authorNicolas CANIART <nicolas.caniart@jobteaser.com>
Sun, 13 Mar 2022 16:39:40 +0000 (12:39 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 14 Mar 2022 19:12:47 +0000 (15:12 -0400)
Fixed issue where using :meth:`.Operations.create_table` in conjunction
with a :class:`.CheckConstraint` that referred to table-bound
:class:`.Column` objects rather than string expressions would be added to
the parent table twice, resulting in an incorrect DDL sequence. Pull
request courtesy Nicolas CANIART.

Fixes: #1004
Closes: #1005
Pull-request: https://github.com/sqlalchemy/alembic/pull/1005
Pull-request-sha: 2fe5c5297bcde990096571a047039c451aa876f6

Change-Id: I2bf48701968fe59a6766f8f8879320b1bfd75774

alembic/operations/schemaobj.py
docs/build/unreleased/1004.rst [new file with mode: 0644]
tests/test_op.py

index c8fab933929fd4bfd7b874a31f79eb6e0b984fc1..0a27920b285b72f08bdb4fa84eb9f6fbac99b600 100644 (file)
@@ -214,7 +214,8 @@ class SchemaObjects:
 
         constraints = [
             sqla_compat._copy(elem, target_table=t)
-            if getattr(elem, "parent", None) is not None
+            if getattr(elem, "parent", None) is not t
+            and getattr(elem, "parent", None) is not None
             else elem
             for elem in columns
             if isinstance(elem, (Constraint, Index))
diff --git a/docs/build/unreleased/1004.rst b/docs/build/unreleased/1004.rst
new file mode 100644 (file)
index 0000000..b6a6908
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, operations
+    :tickets: 1004
+
+    Fixed issue where using :meth:`.Operations.create_table` in conjunction
+    with a :class:`.CheckConstraint` that referred to table-bound
+    :class:`.Column` objects rather than string expressions would be added to
+    the parent table twice, resulting in an incorrect DDL sequence. Pull
+    request courtesy Nicolas CANIART.
index 11d2c1b9af6e08c0e0c12ec1bad7dc7d30814169..677b7bc05b9e11021599eb91da29b4ab8c3b4fb8 100644 (file)
@@ -882,6 +882,27 @@ class OpTest(TestBase):
         ck = [c for c in t1.constraints if isinstance(c, CheckConstraint)]
         eq_(ck[0].name, "ck_1")
 
+    def test_create_table_with_check_constraint_with_expr(self):
+        context = op_fixture()
+        foo_id = Column("foo_id", Integer)
+        t1 = op.create_table(
+            "some_table",
+            Column("id", Integer, primary_key=True),
+            foo_id,
+            CheckConstraint(foo_id > 5, name="ck_1"),
+        )
+        context.assert_(
+            "CREATE TABLE some_table ("
+            "id INTEGER NOT NULL, "
+            "foo_id INTEGER, "
+            "PRIMARY KEY (id), "
+            "CONSTRAINT ck_1 CHECK (foo_id > 5))"
+        )
+
+        ck = [c for c in t1.constraints if isinstance(c, CheckConstraint)]
+        eq_(ck[0].name, "ck_1")
+        eq_(len(ck), 1)
+
     def test_create_table_unique_constraint(self):
         context = op_fixture()
         t1 = op.create_table(