python - 如果涉及抽象表,SQLAlchemy 索引会导致 "Can' t 将未命名列添加到列集合中

标签 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)

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

最佳答案

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

class EntityTable(AbstractTable):
    ...

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

关于python - 如果涉及抽象表,SQLAlchemy 索引会导致 "Can' t 将未命名列添加到列集合中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44570788/

相关文章:

python - 计算 Django ORM 中按查询分组的注释字段的最大总和?

java - 更新方法不会保存到Grails中的数据库

python - 我可以在 SQLAlchemy 上进行动态比较吗?

Python:TypeError:恰好接受 1 个参数(给定 2 个)

Python:继承内置列表类型VS过滤器、map内置函数

python - 如何将构建的 Cython 扩展从一台 PC 转移到另一台?

python - 如何用文件B中的第n行替换文件A中第n个出现的字符?

java - 检查 hibernate 映射类中的不变量

python - SQLAlchemy 附加到关系时出现异常非空失败

postgresql - 使用 GeoAlchemy2 按距离选择和排序。错误的 ST_AsBinary 换行