Index() throws an AttributeError when called with an object using declarative base

Issue #1214 resolved
Former user created an issue

Currently, Index() can't be called with an object using the declarative layer. The result is:

AttributeError: 'InstrumentedAttribute' object has no attribute 'table'

However, using the columns directly works fine, but looks a bit ugly:

Index('viewforum', Topic.__table__.c.forum_id,
      Topic.__table__.c.sticky, Topic.__table__.c.last_post_id)

Many thanks,[BR] Christoph (aka tux21b)

Comments (5)

  1. Mike Bayer repo owner

    though this is problematic; if you say Index('foo', MyClass.foo), its going to force compilation of MyClass' mapper, which can cause problems if dependent classes and tables have not yet been defined. We might further want Index to defer getting the list of columns until create() is called, but then if your mapping has problems, you're going to have ORM errors within metadata.create_all() which is pretty nasty.

    So it might be nice to get change the frigging _CompileOnAttr nastiness to be more of a complete InstrumentedAttribute at mapper construction time. A quick workaround would be to put a working __clause_element__() method on _CompileOnAttr.

  2. Log in to comment