SQLAlchemy索引导致"无法将未命名的列添加到列集合中#34;如果涉及抽象表

时间:2017-06-15 14:58:23

标签: python orm sqlalchemy

我有一个基地"摘要"表和"实体"继承抽象列并添加自己的表:

class AbstractTable(Base):
    __abstract__ = True
    id = Column(Integer, autoincrement=True, primary_key=True)
    name = Column(Unicode)
    value = Column(Unicode)


class EntityTable(AbstractTable):
    entity_id = Column(Integer)
    __table_args__ = ()

我需要entity_idname上的唯一复合索引。致__table_args__,我补充说:

Index('entity_id_name', entity_id, AbstractTable.name, unique=True)

添加完成后,我会收到以下堆栈跟踪:

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "[...]/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 609, in __init__
    DeclarativeMeta.__init__(self, name, bases, d)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", line 64, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 88, in _as_declarative
    _MapperConfig.setup_mapping(cls, classname, dict_)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 103, in setup_mapping
    cfg_cls(cls_, classname, dict_)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 131, in __init__
    self._setup_table()
  File "[...]/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 395, in _setup_table
    **table_kw)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 439, in __new__
    metadata._remove_table(name, schema)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 434, in __new__
    table._init(name, metadata, *args, **kw)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 518, in _init
    self._init_items(*args)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 79, in _init_items
    item._set_parent_with_dispatch(self)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/sql/base.py", line 431, in _set_parent_with_dispatch
    self._set_parent(parent)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 3367, in _set_parent
    ColumnCollectionMixin._set_parent(self, table)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 2611, in _set_parent
    self.columns.add(col)
  File "[...]/lib/python2.7/site-packages/sqlalchemy/sql/base.py", line 503, in add
    "Can't add unnamed column to column collection")
ArgumentError: Can't add unnamed column to column collection

显然它并不是那样的,尽管我认为这应该像写的一样。

如果我将索引定义中的AbstractTable.name更改为字符串'name',我可以使错误无效,如:

Index('entity_id_name', entity_id, 'name', unique=True)

...但我之前从未在索引中使用过字符串列名。我认为我应该能够以这种方式引用抽象列是不正确的吗?

1 个答案:

答案 0 :(得分:1)

是的,不正确。 abstract declarative base classes的情况下继承有效creates copies of found Columns from the parent to child。除此之外,抽象基类中的列未以与具体类相同的方式初始化。所以你引用了错误的类'未初始化的列。将列定义为字符串,或者在EntityTable类之后外部定义索引,如here所述。

class EntityTable(AbstractTable):
    ...

# The class has been constructed and has usable columns.
Index('entity_id_name', EntityTable.entity_id, EntityTable.name, unique=True)