gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:52:39 24 4
gpt4 key购买 nike

我有一个基本的“抽象”表和一个继承抽象列并添加自己的“实体”表:

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com