sqlalchemy:““InstrumentedAttribute”都不是;对象也不是“比较器”;对象有一个属性”错误
我使用 sqlalchemy 和 sqlalchemy-migrate 将表添加到数据库中,当我对命中数据库的不相关代码运行单元测试时,出现以下错误:
Traceback (most recent call last):
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/nose/suite.py", line 208, in run
self.setUp()
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/nose/suite.py", line 291, in setUp
self.setupContext(ancestor)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/nose/suite.py", line 314, in setupContext
try_run(context, names)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/nose/util.py", line 478, in try_run
return func()
File "/Users/lorin/nova/metadata-debugging/nova/tests/__init__.py", line 62, in setup
FLAGS.vpn_start,
File "/Users/lorin/nova/metadata-debugging/nova/network/manager.py", line 577, in create_networks
network_ref = db.network_get_by_cidr(context, cidr)
File "/Users/lorin/nova/metadata-debugging/nova/db/api.py", line 628, in network_get_by_cidr
return IMPL.network_get_by_cidr(context, cidr)
File "/Users/lorin/nova/metadata-debugging/nova/db/sqlalchemy/api.py", line 99, in wrapper
return f(*args, **kwargs)
File "/Users/lorin/nova/metadata-debugging/nova/db/sqlalchemy/api.py", line 1308, in network_get_by_cidr
result = session.query(models.Network).\
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/session.py", line 873, in query
return self._query_cls(entities, self, **kwargs)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/query.py", line 92, in __init__
self._set_entities(entities)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/query.py", line 101, in _set_entities
self._setup_aliasizers(self._entities)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/query.py", line 116, in _setup_aliasizers
_entity_info(entity)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/util.py", line 536, in _entity_info
mapper = mapper.compile()
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/mapper.py", line 805, in compile
mapper._post_configure_properties()
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/mapper.py", line 834, in _post_configure_properties
prop.init()
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/interfaces.py", line 493, in init
self.do_init()
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/properties.py", line 839, in do_init
self._process_dependent_arguments()
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/properties.py", line 883, in _process_dependent_arguments
setattr(self, attr, getattr(self, attr)())
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/ext/declarative.py", line 1078, in return_cls
x = eval(arg, globals(), d)
File "<string>", line 1, in <module>
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/util.py", line 76, in __missing__
self[key] = val = self.creator(key)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/ext/declarative.py", line 1070, in access_cls
elif key in cls.metadata.tables:
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/attributes.py", line 138, in __getattr__
key)
AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object has an attribute 'tables'
我可以做什么来导致我的 sqlalchemy 模型中出现错误这个错误?该代码位于 Launchpad 上 lp:~usc-isi/nova/instance_type_metadata ,修订版 1109-1115 有我的更改。
我向 添加了一个模型nova/db/sqlalchemy/models.py:
class InstanceTypeMetadata(BASE, NovaBase):
"""Represents a metadata key/value pair for an instance_type"""
__tablename__ = 'instance_type_metadata'
id = Column(Integer, primary_key=True)
key = Column(String(255))
value = Column(String(255))
instance_type_id = Column(Integer, ForeignKey('instance_types.id'),
nullable=False)
instance_type = relationship(InstanceTypes, backref="metadata",
foreign_keys=instance_type_id,
primaryjoin='and_('
'InstanceTypeMetadata.instance_type_id == InstanceTypes.id,'
'InstanceTypeMetadata.deleted == False)')
我添加了文件 nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py
I've added a table to a database using sqlalchemy and sqlalchemy-migrate, and when I run unit tests on unrelated code that hits the database, I get the following error:
Traceback (most recent call last):
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/nose/suite.py", line 208, in run
self.setUp()
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/nose/suite.py", line 291, in setUp
self.setupContext(ancestor)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/nose/suite.py", line 314, in setupContext
try_run(context, names)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/nose/util.py", line 478, in try_run
return func()
File "/Users/lorin/nova/metadata-debugging/nova/tests/__init__.py", line 62, in setup
FLAGS.vpn_start,
File "/Users/lorin/nova/metadata-debugging/nova/network/manager.py", line 577, in create_networks
network_ref = db.network_get_by_cidr(context, cidr)
File "/Users/lorin/nova/metadata-debugging/nova/db/api.py", line 628, in network_get_by_cidr
return IMPL.network_get_by_cidr(context, cidr)
File "/Users/lorin/nova/metadata-debugging/nova/db/sqlalchemy/api.py", line 99, in wrapper
return f(*args, **kwargs)
File "/Users/lorin/nova/metadata-debugging/nova/db/sqlalchemy/api.py", line 1308, in network_get_by_cidr
result = session.query(models.Network).\
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/session.py", line 873, in query
return self._query_cls(entities, self, **kwargs)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/query.py", line 92, in __init__
self._set_entities(entities)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/query.py", line 101, in _set_entities
self._setup_aliasizers(self._entities)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/query.py", line 116, in _setup_aliasizers
_entity_info(entity)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/util.py", line 536, in _entity_info
mapper = mapper.compile()
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/mapper.py", line 805, in compile
mapper._post_configure_properties()
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/mapper.py", line 834, in _post_configure_properties
prop.init()
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/interfaces.py", line 493, in init
self.do_init()
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/properties.py", line 839, in do_init
self._process_dependent_arguments()
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/properties.py", line 883, in _process_dependent_arguments
setattr(self, attr, getattr(self, attr)())
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/ext/declarative.py", line 1078, in return_cls
x = eval(arg, globals(), d)
File "<string>", line 1, in <module>
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/util.py", line 76, in __missing__
self[key] = val = self.creator(key)
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/ext/declarative.py", line 1070, in access_cls
elif key in cls.metadata.tables:
File "/Users/lorin/nova/instance_type_metadata/.nova-venv/lib/python2.6/site-packages/sqlalchemy/orm/attributes.py", line 138, in __getattr__
key)
AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object has an attribute 'tables'
What could I have done to cause wrong in my sqlalchemy models to cause this error? The code is on Launchpad at lp:~usc-isi/nova/instance_type_metadata, revisions 1109-1115 have my changes.
I added one model to nova/db/sqlalchemy/models.py:
class InstanceTypeMetadata(BASE, NovaBase):
"""Represents a metadata key/value pair for an instance_type"""
__tablename__ = 'instance_type_metadata'
id = Column(Integer, primary_key=True)
key = Column(String(255))
value = Column(String(255))
instance_type_id = Column(Integer, ForeignKey('instance_types.id'),
nullable=False)
instance_type = relationship(InstanceTypes, backref="metadata",
foreign_keys=instance_type_id,
primaryjoin='and_('
'InstanceTypeMetadata.instance_type_id == InstanceTypes.id,'
'InstanceTypeMetadata.deleted == False)')
And I added the file nova/db/sqlalchemy/migrate_repo/versions/019_add_instance_type_metadata.py
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您从映射类的回溯属性
metadata
中看到的那样,它由 SQLAlchemy 的MetaData
实例的声明性扩展在内部使用,而您将其替换为您自己的关系 (backref= “元数据”
)具有不同的含义。将其重命名为其他名称应该可以解决问题。As you can see from traceback attribute
metadata
of mapped class is used internally by declarative extension for SQLAlchemy'sMetaData
instance, while you replaced it with your own relation (backref="metadata"
) with different meaning. Renaming it to something else should solve the problem.