]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug in new :paramref:`.MetaData.naming_convention` feature
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 12 Mar 2014 18:46:55 +0000 (14:46 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 12 Mar 2014 18:46:55 +0000 (14:46 -0400)
where the name of a check constraint making use of the
`"%(constraint_name)s"` token would get doubled up for the
constraint generated by a boolean or enum type, and overall
duplicate events would cause the `"%(constraint_name)s"` token
to keep compounding itself.
fixes #2991

doc/build/changelog/changelog_09.rst
lib/sqlalchemy/sql/naming.py
lib/sqlalchemy/sql/sqltypes.py
test/sql/test_metadata.py

index fc39ea347a626fc08cac0eae6c1b433260b985a0..2a8c24f91ef0bb0e06d4098fde6f20d0f65e132c 100644 (file)
 .. changelog::
     :version: 0.9.4
 
+    .. change::
+        :tags: bug, sql
+        :tickets: 2991
+
+        Fixed bug in new :paramref:`.MetaData.naming_convention` feature
+        where the name of a check constraint making use of the
+        `"%(constraint_name)s"` token would get doubled up for the
+        constraint generated by a boolean or enum type, and overall
+        duplicate events would cause the `"%(constraint_name)s"` token
+        to keep compounding itself.
+
     .. change::
         :tags: feature, orm
 
index ee99ccbcef15991dd5267589952e74c10ea02a40..bbb8431211ea74092acbf33e3dacb2b2db458c2f 100644 (file)
@@ -22,7 +22,7 @@ class ConventionDict(object):
         self._is_fk = isinstance(const, ForeignKeyConstraint)
         self.table = table
         self.convention = convention
-        self._const_name = const.name
+        self._const_name = const._orig_name = getattr(const, '_orig_name', const.name)
 
     def _key_table_name(self):
         return self.table.name
index fee424e659cb81ce2f5ddbfcd7a5e8f61a23aafc..ec1d66459cac4137f66ec024d7376b9565758f4b 100644 (file)
@@ -1131,7 +1131,7 @@ class Enum(String, SchemaType):
                         _create_rule=util.portable_instancemethod(
                                         self._should_create_constraint)
                     )
-        table.append_constraint(e)
+        assert e.table is table
 
     def adapt(self, impltype, **kw):
         schema = kw.pop('schema', self.schema)
@@ -1268,7 +1268,7 @@ class Boolean(TypeEngine, SchemaType):
                         _create_rule=util.portable_instancemethod(
                                     self._should_create_constraint)
                     )
-        table.append_constraint(e)
+        assert e.table is table
 
     @property
     def python_type(self):
index ce95634fdc9411c3a380ec6e7b162a593702764e..fd166bc17a5c2e743bea596a65a2b0000a49631a 100644 (file)
@@ -7,7 +7,7 @@ from sqlalchemy import Integer, String, UniqueConstraint, \
     CheckConstraint, ForeignKey, MetaData, Sequence, \
     ForeignKeyConstraint, PrimaryKeyConstraint, ColumnDefault, Index, event,\
     events, Unicode, types as sqltypes, bindparam, \
-    Table, Column
+    Table, Column, Boolean, Enum
 from sqlalchemy import schema, exc
 import sqlalchemy as tsa
 from sqlalchemy.testing import fixtures
@@ -2731,3 +2731,39 @@ class NamingConventionTest(fixtures.TestBase):
                         ['user.id', 'user.version'])
         a1.append_constraint(fk)
         eq_(fk.name, "fk_address_HASH_address")
+
+    def test_schematype_ck_name_boolean(self):
+        m1 = MetaData(naming_convention={
+                            "ck": "ck_%(table_name)s_%(constraint_name)s"})
+
+        u1 = Table('user', m1,
+            Column('x', Boolean(name='foo'))
+            )
+        eq_(
+            [c for c in u1.constraints
+                if isinstance(c, CheckConstraint)][0].name, "ck_user_foo"
+        )
+
+    def test_schematype_ck_name_enum(self):
+        m1 = MetaData(naming_convention={
+                            "ck": "ck_%(table_name)s_%(constraint_name)s"})
+
+        u1 = Table('user', m1,
+            Column('x', Enum('a', 'b', name='foo'))
+            )
+        eq_(
+            [c for c in u1.constraints
+                if isinstance(c, CheckConstraint)][0].name, "ck_user_foo"
+        )
+
+    def test_ck_constraint_redundant_event(self):
+        u1 = self._fixture(naming_convention={
+                            "ck": "ck_%(table_name)s_%(constraint_name)s"})
+
+        ck1 = CheckConstraint(u1.c.version > 3, name='foo')
+        u1.append_constraint(ck1)
+        u1.append_constraint(ck1)
+        u1.append_constraint(ck1)
+
+        eq_(ck1.name, "ck_user_foo")
+