summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2023-04-07 11:05:20 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2023-04-07 11:05:20 -0400
commit1ae44556907f14f92874f05d05242cb57bb0f855 (patch)
tree5e744b2c671fdf74be1b613cd5bc145f081fd213
parent3d9b1128cd6bf03ecb45003587c0eedfb9552b07 (diff)
downloadalembic-1ae44556907f14f92874f05d05242cb57bb0f855.tar.gz
uniquify cols for FK table object
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
-rw-r--r--alembic/operations/schemaobj.py5
-rw-r--r--docs/build/unreleased/1215.rst9
-rw-r--r--tests/test_op.py15
3 files changed, 28 insertions, 1 deletions
diff --git a/alembic/operations/schemaobj.py b/alembic/operations/schemaobj.py
index 0568471..799f113 100644
--- a/alembic/operations/schemaobj.py
+++ b/alembic/operations/schemaobj.py
@@ -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
index 0000000..c2c3a08
--- /dev/null
+++ b/docs/build/unreleased/1215.rst
@@ -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.
diff --git a/tests/test_op.py b/tests/test_op.py
index c483c4a..8ae22a0 100644
--- a/tests/test_op.py
+++ b/tests/test_op.py
@@ -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"])