"""
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
@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:
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_"