diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-10-11 15:55:57 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-10-11 15:55:57 -0400 |
| commit | 9d38ed33400adf3ba8fdf3af49f26de1270bbe23 (patch) | |
| tree | be88cbfb8654fc5cb119e05a0829c2111b966de2 | |
| parent | 5f0b864ad02409cf668fa7db5cbac99ac6ffc329 (diff) | |
| download | sqlalchemy-9d38ed33400adf3ba8fdf3af49f26de1270bbe23.tar.gz | |
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]
| -rw-r--r-- | doc/build/changelog/changelog_09.rst | 9 | ||||
| -rw-r--r-- | lib/sqlalchemy/sql/schema.py | 7 | ||||
| -rw-r--r-- | 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 fc4f79f4c..c784a39eb 100644 --- a/doc/build/changelog/changelog_09.rst +++ b/doc/build/changelog/changelog_09.rst @@ -13,6 +13,15 @@ :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 92220b0d1..c493b9132 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 2498a822c..1da833d9f 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' ] ) |
