diff options
-rw-r--r-- | alembic/operations/schemaobj.py | 5 | ||||
-rw-r--r-- | docs/build/unreleased/1215.rst | 9 | ||||
-rw-r--r-- | tests/test_op.py | 15 |
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"]) |