diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-02-04 12:09:54 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-02-04 12:11:41 -0500 |
| commit | 9660e94e35be438b0d51cd87e6ccb4047a332c15 (patch) | |
| tree | 0a888b4fb18430618bca045b3c2456dff893f6cb /test/sql | |
| parent | e91d918fc24d678a413feb3744e70014f54172c7 (diff) | |
| download | sqlalchemy-9660e94e35be438b0d51cd87e6ccb4047a332c15.tar.gz | |
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
Diffstat (limited to 'test/sql')
| -rw-r--r-- | test/sql/test_metadata.py | 67 |
1 files changed, 67 insertions, 0 deletions
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"} |
