]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
uniquify cols for FK table object
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 7 Apr 2023 15:05:20 +0000 (11:05 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 7 Apr 2023 15:05:20 +0000 (11:05 -0400)
Fixed issue where using a directive such as ``op.create_foreign_key()`` to
create a self-referential constraint on a single table where the same
column were present on both sides (e.g. within a composite foreign key)
would produce an error under SQLAlchemy 2.0 and a warning under SQLAlchemy
1.4 indicating that a duplicate column were being added to a table.

Change-Id: I2a8f5d8def2714792bffcdfb8bf88a5080ec8ce7
Fixes: #1215
alembic/operations/schemaobj.py
docs/build/unreleased/1215.rst [new file with mode: 0644]
tests/test_op.py

index 0568471a76d520febeefe8b12e2344b4381de139..799f1139d93b30867d9f471ef987e4f19d6a9b4f 100644 (file)
@@ -89,7 +89,10 @@ class SchemaObjects:
         t1 = sa_schema.Table(
             source,
             m,
-            *[sa_schema.Column(n, NULLTYPE) for n in t1_cols],
+            *[
+                sa_schema.Column(n, NULLTYPE)
+                for n in util.unique_list(t1_cols)
+            ],
             schema=source_schema,
         )
 
diff --git a/docs/build/unreleased/1215.rst b/docs/build/unreleased/1215.rst
new file mode 100644 (file)
index 0000000..c2c3a08
--- /dev/null
@@ -0,0 +1,9 @@
+.. change::
+    :tags: bug, operations
+    :tickets: 1215
+
+    Fixed issue where using a directive such as ``op.create_foreign_key()`` to
+    create a self-referential constraint on a single table where the same
+    column were present on both sides (e.g. within a composite foreign key)
+    would produce an error under SQLAlchemy 2.0 and a warning under SQLAlchemy
+    1.4 indicating that a duplicate column were being added to a table.
index c483c4a8b9261c4e4ffe157d4220c1b17a15103b..8ae22a030860a63063a08b83e70fe9d2ed6c3cfd 100644 (file)
@@ -729,6 +729,21 @@ class OpTest(TestBase):
             "FOREIGN KEY(foo) REFERENCES t1 (bar)"
         )
 
+    def test_add_foreign_key_composite_self_referential(self):
+        """test #1215
+
+        the same column name is present on both sides.
+
+        """
+        context = op_fixture()
+        op.create_foreign_key(
+            "fk_test", "t1", "t1", ["foo", "bar"], ["bat", "bar"]
+        )
+        context.assert_(
+            "ALTER TABLE t1 ADD CONSTRAINT fk_test "
+            "FOREIGN KEY(foo, bar) REFERENCES t1 (bat, bar)"
+        )
+
     def test_add_primary_key_constraint(self):
         context = op_fixture()
         op.create_primary_key("pk_test", "t1", ["foo", "bar"])