diff options
| -rw-r--r-- | doc/build/changelog/unreleased_14/7958.rst | 20 | ||||
| -rw-r--r-- | test/sql/test_metadata.py | 23 |
2 files changed, 43 insertions, 0 deletions
diff --git a/doc/build/changelog/unreleased_14/7958.rst b/doc/build/changelog/unreleased_14/7958.rst new file mode 100644 index 000000000..057647bd8 --- /dev/null +++ b/doc/build/changelog/unreleased_14/7958.rst @@ -0,0 +1,20 @@ +.. change:: + :tags: bug, schema + :tickets: 7958 + + Fixed bug where :class:`.ForeignKeyConstraint` naming conventions using the + ``referred_column_0`` naming convention key would not work if the foreign + key constraint were set up as a :class:`.ForeignKey` object rather than an + explicit :class:`.ForeignKeyConstraint` object. As this change makes use of + a backport of some fixes from version 2.0, an additional little-known + feature that has likely been broken for many years is also fixed which is + that a :class:`.ForeignKey` object may refer to a referred table by name of + the table alone without using a column name, if the name of the referent + column is the same as that of the referred column. + + The ``referred_column_0`` naming convention key was not previously not + tested with the :class:`.ForeignKey` object, only + :class:`.ForeignKeyConstraint`, and this bug reveals that the feature has + never worked correctly unless :class:`.ForeignKeyConstraint` is used for + all FK constraints. This bug traces back to the original introduction of + the feature introduced for :ticket:`3989`. diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index f7480d11a..b175f9663 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -5345,6 +5345,29 @@ class NamingConventionTest(fixtures.TestBase, AssertsCompiledSQL): a1.append_constraint(fk) eq_(fk.name, "fk_address_user_id_user_id") + @testing.combinations(True, False, argnames="col_has_type") + def test_fk_ref_local_referent_has_no_type(self, col_has_type): + """test #7958""" + + metadata = MetaData( + naming_convention={ + "fk": "fk_%(referred_column_0_name)s", + } + ) + Table("a", metadata, Column("id", Integer, primary_key=True)) + b = Table( + "b", + metadata, + Column("id", Integer, primary_key=True), + Column("aid", ForeignKey("a.id")) + if not col_has_type + else Column("aid", Integer, ForeignKey("a.id")), + ) + fks = list( + c for c in b.constraints if isinstance(c, ForeignKeyConstraint) + ) + eq_(fks[0].name, "fk_id") + def test_custom(self): def key_hash(const, table): return "HASH_%s" % table.name |
