]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
The "name" attribute is set on :class:`.Index` before the "attach"
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 11 Oct 2013 19:55:57 +0000 (15:55 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 11 Oct 2013 19:55:57 +0000 (15:55 -0400)
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
lib/sqlalchemy/sql/schema.py
test/sql/test_metadata.py

index fc4f79f4c8084381b31d4fdec683724656b9e6d1..c784a39ebe26a297a9eff79c487a9aa8ff214875 100644 (file)
 .. 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
index 92220b0d111ca652418111a48e65b3f9e6564eb9..c493b9132088f48f1c1b08f95d015b35615d9d65 100644 (file)
@@ -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)
index 2498a822c0a7f21b0027def4b773d1052ce7680b..1da833d9f46a6f5593877dac86613328aa7c4a07 100644 (file)
@@ -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'
             ]
         )