From: Mike Bayer Date: Fri, 7 Apr 2023 15:05:20 +0000 (-0400) Subject: uniquify cols for FK table object X-Git-Tag: rel_1_10_4~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ae44556907f14f92874f05d05242cb57bb0f855;p=thirdparty%2Fsqlalchemy%2Falembic.git 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 --- diff --git a/alembic/operations/schemaobj.py b/alembic/operations/schemaobj.py index 0568471a..799f1139 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 00000000..c2c3a08c --- /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 c483c4a8..8ae22a03 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"])