From 9d38ed33400adf3ba8fdf3af49f26de1270bbe23 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Fri, 11 Oct 2013 15:55:57 -0400 Subject: [PATCH] The "name" attribute is set on :class:`.Index` before the "attach" events are called, so that attachment events can be used to dynamically generate a name for the index based on the parent table and/or columns. [ticket:2835] --- doc/build/changelog/changelog_09.rst | 9 +++++++++ lib/sqlalchemy/sql/schema.py | 7 ++++--- test/sql/test_metadata.py | 13 +++++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/doc/build/changelog/changelog_09.rst b/doc/build/changelog/changelog_09.rst index fc4f79f4c8..c784a39ebe 100644 --- a/doc/build/changelog/changelog_09.rst +++ b/doc/build/changelog/changelog_09.rst @@ -12,6 +12,15 @@ .. changelog:: :version: 0.9.0 + .. change:: + :tags: bug, sql + :tickets: 2835 + + The "name" attribute is set on :class:`.Index` before the "attach" + events are called, so that attachment events can be used to dynamically + generate a name for the index based on the parent table and/or + columns. + .. change:: :tags: bug, engine :tickets: 2748 diff --git a/lib/sqlalchemy/sql/schema.py b/lib/sqlalchemy/sql/schema.py index 92220b0d11..c493b91320 100644 --- a/lib/sqlalchemy/sql/schema.py +++ b/lib/sqlalchemy/sql/schema.py @@ -2600,14 +2600,15 @@ class Index(ColumnCollectionMixin, SchemaItem): columns.append(expr) self.expressions = expressions + self.name = quoted_name(name, kw.pop("quote", None)) + self.unique = kw.pop('unique', False) + self.kwargs = kw # will call _set_parent() if table-bound column # objects are present ColumnCollectionMixin.__init__(self, *columns) - self.name = quoted_name(name, kw.pop("quote", None)) - self.unique = kw.pop('unique', False) - self.kwargs = kw + def _set_parent(self, table): ColumnCollectionMixin._set_parent(self, table) diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 2498a822c0..1da833d9f4 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -1904,6 +1904,7 @@ class CatchAllEventsTest(fixtures.TestBase): parent.__class__.__name__)) def after_attach(obj, parent): + assert hasattr(obj, 'name') # so we can change it canary.append("%s->%s" % (target.__name__, parent)) event.listen(target, "before_parent_attach", before_attach) event.listen(target, "after_parent_attach", after_attach) @@ -1911,14 +1912,15 @@ class CatchAllEventsTest(fixtures.TestBase): for target in [ schema.ForeignKeyConstraint, schema.PrimaryKeyConstraint, schema.UniqueConstraint, - schema.CheckConstraint + schema.CheckConstraint, + schema.Index ]: evt(target) m = MetaData() Table('t1', m, Column('id', Integer, Sequence('foo_id'), primary_key=True), - Column('bar', String, ForeignKey('t2.id')), + Column('bar', String, ForeignKey('t2.id'), index=True), Column('bat', Integer, unique=True), ) Table('t2', m, @@ -1926,17 +1928,20 @@ class CatchAllEventsTest(fixtures.TestBase): Column('bar', Integer), Column('bat', Integer), CheckConstraint("bar>5"), - UniqueConstraint('bar', 'bat') + UniqueConstraint('bar', 'bat'), + Index(None, 'bar', 'bat') ) eq_( canary, [ 'PrimaryKeyConstraint->Table', 'PrimaryKeyConstraint->t1', + 'Index->Table', 'Index->t1', 'ForeignKeyConstraint->Table', 'ForeignKeyConstraint->t1', 'UniqueConstraint->Table', 'UniqueConstraint->t1', 'PrimaryKeyConstraint->Table', 'PrimaryKeyConstraint->t2', 'CheckConstraint->Table', 'CheckConstraint->t2', - 'UniqueConstraint->Table', 'UniqueConstraint->t2' + 'UniqueConstraint->Table', 'UniqueConstraint->t2', + 'Index->Table', 'Index->t2' ] ) -- 2.47.3