declarative index does not work

Issue #1466 resolved
Former user created an issue

I am trying to configure a multi-column index on a table, but always seem to run into a SQLAlchemy exception. This is a fairly minimal test case:

from sqlalchemy import MetaData
from sqlalchemy import schema
from sqlalchemy import types
from sqlalchemy.ext.declarative import declarative_base

metadata = MetaData()
BaseObject = declarative_base(metadata=metadata)

class ImageScale(BaseObject):
    __tablename__ = "image_scale"

    id = schema.Column(types.Integer(), primary_key=True, autoincrement=True)
    width = schema.Column(types.Integer(), nullable=False)
    height = schema.Column(types.Integer(), nullable=False)
    scale_direction = schema.Column(types.String(16), nullable=False)
    __table_args__ = (schema.Index("scale_idx", width, height, scale_direction), {})

running that results in this error:

Traceback (most recent call last):
  File "../../bin/python", line 69, in <module>
    execfile(sys.argv[0](0))
  File "/tmp/x.py", line 9, in <module>
    class ImageScale(BaseObject):
  File "/tmp/x.py", line 16, in ImageScale
    __table_args__ = (schema.Index("scale_idx", width, height, scale_direction), {})
  File "/Users/wichert/Library/eggs/SQLAlchemy-0.5.4p2-py2.6.egg/sqlalchemy/schema.py", line 1461, in __init__
    self._init_items(*columns)
  File "/Users/wichert/Library/eggs/SQLAlchemy-0.5.4p2-py2.6.egg/sqlalchemy/schema.py", line 1465, in _init_items
    self.append_column(_to_schema_column(column))
  File "/Users/wichert/Library/eggs/SQLAlchemy-0.5.4p2-py2.6.egg/sqlalchemy/schema.py", line 1476, in append_column
    self._set_parent(column.table)
  File "/Users/wichert/Library/eggs/SQLAlchemy-0.5.4p2-py2.6.egg/sqlalchemy/schema.py", line 1469, in _set_parent
    self.metadata = table.metadata
AttributeError: 'NoneType' object has no attribute 'metadata'

Comments (4)

  1. Former user Account Deleted

    How does one do that with declarative? I could not find any example in the documentation.

  2. Mike Bayer repo owner

    there is no difference.

    class MyClass(Base):
        __tablename__ = "whatever"
        id = Column(...)
    
    
    Index("idx_myclass", MyClass.foo, MyClass.bar)
    
  3. Log in to comment