diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-03-12 15:09:48 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-03-12 15:09:48 -0400 |
| commit | a7ef7eccaacae5341bb03a58cc0538718c33c329 (patch) | |
| tree | 871d09578e735ccca94d8a3cdf072c998d394d30 | |
| parent | d85d6f9a3f1d3132abcd917d4378b2c4e14aec65 (diff) | |
| download | sqlalchemy-a7ef7eccaacae5341bb03a58cc0538718c33c329.tar.gz | |
:paramref:`.MetaData.naming_convention` feature will now also
apply to :class:`.CheckConstraint` objects that are associated
directly with a :class:`.Column` instead of just on the
:class:`.Table`.
| -rw-r--r-- | doc/build/changelog/changelog_09.rst | 8 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/naming.py | 11 | ||||
| -rw-r--r-- | test/sql/test_metadata.py | 17 |
3 files changed, 34 insertions, 2 deletions
diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst index 2a8c24f91..20a0b656d 100644 --- a/doc/build/changelog/changelog_09.rst +++ b/doc/build/changelog/changelog_09.rst @@ -16,6 +16,14 @@ .. change:: :tags: bug, sql + + :paramref:`.MetaData.naming_convention` feature will now also + apply to :class:`.CheckConstraint` objects that are associated + directly with a :class:`.Column` instead of just on the + :class:`.Table`. + + .. change:: + :tags: bug, sql :tickets: 2991 Fixed bug in new :paramref:`.MetaData.naming_convention` feature diff --git a/lib/sqlalchemy/sql/naming.py b/lib/sqlalchemy/sql/naming.py index bbb843121..3ba7f5105 100644 --- a/lib/sqlalchemy/sql/naming.py +++ b/lib/sqlalchemy/sql/naming.py @@ -10,7 +10,7 @@ """ from .schema import Constraint, ForeignKeyConstraint, PrimaryKeyConstraint, \ - UniqueConstraint, CheckConstraint, Index, Table + UniqueConstraint, CheckConstraint, Index, Table, Column from .. import event, events from .. import exc from .elements import _truncated_label @@ -107,7 +107,14 @@ def _get_convention(dict_, key): @event.listens_for(Constraint, "after_parent_attach") @event.listens_for(Index, "after_parent_attach") def _constraint_name(const, table): - if isinstance(table, Table): + if isinstance(table, Column): + # for column-attached constraint, set another event + # to link the column attached to the table as this constraint + # associated with the table. + event.listen(table, "after_parent_attach", + lambda col, table: _constraint_name(const, table) + ) + elif isinstance(table, Table): metadata = table.metadata convention = _get_convention(metadata.naming_convention, type(const)) if convention is not None: diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index fd166bc17..978f4f1f4 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -2679,6 +2679,23 @@ class NamingConventionTest(fixtures.TestBase): CheckConstraint, u1.c.data == 'x' ) + def test_column_attached_ck_name(self): + m = MetaData(naming_convention={ + "ck": "ck_%(table_name)s_%(constraint_name)s" + }) + ck = CheckConstraint('x > 5', name='x1') + Table('t', m, Column('x', ck)) + eq_(ck.name, "ck_t_x1") + + def test_table_attached_ck_name(self): + m = MetaData(naming_convention={ + "ck": "ck_%(table_name)s_%(constraint_name)s" + }) + ck = CheckConstraint('x > 5', name='x1') + Table('t', m, Column('x', Integer), ck) + eq_(ck.name, "ck_t_x1") + + def test_fk_name_schema(self): u1 = self._fixture(naming_convention={ "fk": "fk_%(table_name)s_%(column_0_name)s_" |
