From 9660e94e35be438b0d51cd87e6ccb4047a332c15 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 4 Feb 2021 12:09:54 -0500 Subject: Accommodate column-based naming conventions for pk constraint Repaired / implemented support for primary key constraint naming conventions that use column names/keys/etc as part of the convention. In particular, this includes that the :class:`.PrimaryKeyConstraint` object that's automatically associated with a :class:`.schema.Table` will update its name as new primary key :class:`_schema.Column` objects are added to the table and then to the constraint. Internal failure modes related to this constraint construction process including no columns present, no name present or blank name present are now accommodated. Fixes: #5919 Change-Id: Ic2800b50f4a4cd5978bec48cefea0a2e198e0123 --- test/sql/test_metadata.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) (limited to 'test/sql') diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 4a592a476..1c24cc095 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -4841,6 +4841,73 @@ class NamingConventionTest(fixtures.TestBase, AssertsCompiledSQL): AddConstraint(const[0]), "ALTER TABLE foo ADD UNIQUE (id)" ) + @testing.combinations( + ("nopk",), + ("column",), + ("constraint",), + ("explicit_name",), + argnames="pktype", + ) + @testing.combinations( + ("pk_%(table_name)s", "pk_t1"), + ("pk_%(column_0_name)s", "pk_x"), + ("pk_%(column_0_N_name)s", "pk_x_y"), + ("pk_%(column_0_N_label)s", "pk_t1_x_t1_y"), + ("%(column_0_name)s", "x"), + ("%(column_0N_name)s", "xy"), + argnames="conv, expected_name", + ) + def test_pk_conventions(self, conv, expected_name, pktype): + m1 = MetaData(naming_convention={"pk": conv}) + + if pktype == "column": + t1 = Table( + "t1", + m1, + Column("x", Integer, primary_key=True), + Column("y", Integer, primary_key=True), + ) + elif pktype == "constraint": + t1 = Table( + "t1", + m1, + Column("x", Integer), + Column("y", Integer), + PrimaryKeyConstraint("x", "y"), + ) + elif pktype == "nopk": + t1 = Table( + "t1", + m1, + Column("x", Integer, nullable=False), + Column("y", Integer, nullable=False), + ) + expected_name = None + elif pktype == "explicit_name": + t1 = Table( + "t1", + m1, + Column("x", Integer, primary_key=True), + Column("y", Integer, primary_key=True), + PrimaryKeyConstraint("x", "y", name="myname"), + ) + expected_name = "myname" + + if expected_name: + eq_(t1.primary_key.name, expected_name) + + if pktype == "nopk": + self.assert_compile( + schema.CreateTable(t1), + "CREATE TABLE t1 (x INTEGER NOT NULL, y INTEGER NOT NULL)", + ) + else: + self.assert_compile( + schema.CreateTable(t1), + "CREATE TABLE t1 (x INTEGER NOT NULL, y INTEGER NOT NULL, " + "CONSTRAINT %s PRIMARY KEY (x, y))" % expected_name, + ) + def test_uq_name(self): u1 = self._fixture( naming_convention={"uq": "uq_%(table_name)s_%(column_0_name)s"} -- cgit v1.2.1