2017-06-15 34 views
0

我有一個基地「抽象的」表和繼承抽象列,並增加了自己的一個「實體」表: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

是的,是不正確的。在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) 
相關問題