我在下面的代码中收到以下错误。我没有看到返回表/对象的点,或者至少应该返回。任何其他指针也会受到欢迎。
我跑
OrmConfig.session().query(Aquarium).all()
给出的错误是:
InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Original exception was: 'Table' object has no attribute 'datetime'
基于此代码:
#################################################################
class Aquarium(OrmConfig.base):
__tablename__ = 'aquarium'
# Special attribute used for JSON encoding/decoding
_type = 'aquapi.entity.Aquarium'
id = Column(Integer, Sequence('seq_aquarium_id'), primary_key=True)
name = Column(Unicode(50))
created = Column(DateTime())
updated = Column(DateTime())
sensors = relationship("Sensor", backref=backref('aquarium'))
#################################################################
class SensorType(OrmConfig.base):
__tablename__ = 'sensor_type';
# Special attribute used for JSON encoding/decoding
_type = 'aquapi.entity.SensorType'
id = Column(Integer, Sequence('seq_sensor_type_id'), primary_key=True)
name = Column(Unicode(30))
#################################################################
class SensorValue(OrmConfig.base):
__tablename__ = 'sensor_value';
# Special attribute used for JSON encoding/decoding
_type = 'aquapi.entity.SensorValue'
id = Column(Integer, Sequence('seq_sensorvalue_id'), primary_key=True)
sensor_id = Column(Integer, ForeignKey('sensor.id'))
value = Column(Unicode(30))
datetime = Column(DateTime())
#################################################################
class Sensor(OrmConfig.base):
__tablename__ = 'sensor';
# Special attribute used for JSON encoding/decoding
_type = 'aquapi.entity.Sensor'
id = Column(Integer, Sequence('seq_sensor_id'), primary_key=True)
name = Column(Unicode(30))
enabled = Column(Boolean)
freq = Column(Integer)
interface = Column(Unicode(10))
aquarium_id = Column(Integer, ForeignKey('aquarium.id'))
type_id = Column(Integer, ForeignKey('sensor_type.id'))
type = relationship(SensorType, backref=backref('sensors', uselist=False))
values = relationship(SensorValue, backref=backref('sensor_value', order_by='sensor_value.datetime'))
这是完整的追溯
[13/Jan/2014:12:01:39] ENGINE Serving on 0.0.0.0:8282
[13/Jan/2014:12:01:39] ENGINE Bus STARTED
[13/Jan/2014:12:01:40] HTTP Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.4-py2.7.egg/cherrypy/_cprequest.py", line 656, in respond
response.body = self.handler()
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.4-py2.7.egg/cherrypy/lib/encoding.py", line 188, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.4-py2.7.egg/cherrypy/lib/jsontools.py", line 61, in json_handler
value = cherrypy.serving.request._json_inner_handler(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/CherryPy-3.2.4-py2.7.egg/cherrypy/_cpdispatch.py", line 34, in __call__
return self.callable(*self.args, **self.kwargs)
File "/home/patrick/workspace/AquaPi - Backend/aquapi/api_service.py", line 14, in default
"items": json.tojson(manager.Aquarium().read_all())
File "/home/patrick/workspace/AquaPi - Backend/aquapi/manager.py", line 19, in read_all
return OrmConfig.session().query(self.entity_type).all()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 969, in query
return self._query_cls(entities, self, **kwargs)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 107, in __init__
self._set_entities(entities)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 116, in _set_entities
self._setup_aliasizers(self._entities)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 131, in _setup_aliasizers
_entity_info(entity)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/util.py", line 550, in _entity_info
mapperlib.configure_mappers()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 2861, in configure_mappers
mapper._post_configure_properties()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1166, in _post_configure_properties
prop.init()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/interfaces.py", line 128, in init
self.do_init()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/properties.py", line 919, in do_init
self._generate_backref()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/properties.py", line 1418, in _generate_backref
mapper._configure_property(backref_key, relationship)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1145, in _configure_property
prop.init()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/interfaces.py", line 128, in init
self.do_init()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/properties.py", line 913, in do_init
self._process_dependent_arguments()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/properties.py", line 969, in _process_dependent_arguments
setattr(self, attr, attr_value())
File "/usr/lib/python2.7/dist-packages/sqlalchemy/ext/declarative.py", line 1346, in return_cls
x = eval(arg, globals(), d)
File "<string>", line 1, in <module>
AttributeError: 'Table' object has no attribute 'datetime'
127.0.0.1 - - [13/Jan/2014:12:01:40] "GET /aquarium HTTP/1.1" 500 805 "" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:26.0) Gecko/20100101 Firefox/26.0"
答案 0 :(得分:1)
改变它:
values = relationship(SensorValue, backref=backref('sensor_value', order_by='sensor_value.datetime'))
要:
values = relationship(SensorValue, backref=backref('sensor_value', order_by='SensorValue.datetime'))