我正在嘗試將一些模型重構爲Joined Table Inheritance。我能夠使配置在我們的模型庫中工作,但是我們的API具有類似的模型並沒有取得任何成功。我遇到的錯誤發生在Flask應用嘗試以任何形式與模型進行交互的時刻,而不僅僅是繼承表。配置繼承時出錯SQLAlchemy
堆棧跟蹤
Traceback (most recent call last):
File ".../lib/python3.5/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File ".../lib/python3.5/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File ".../lib/python3.5/site-packages/flask_restful/__init__.py", line 270, in error_router
return original_handler(e)
File ".../lib/python3.5/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File ".../lib/python3.5/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File ".../lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File ".../lib/python3.5/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File ".../lib/python3.5/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File ".../lib/python3.5/site-packages/flask_restful/__init__.py", line 270, in error_router
return original_handler(e)
File ".../lib/python3.5/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File ".../lib/python3.5/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File ".../lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File ".../lib/python3.5/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File ".../lib/python3.5/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File ".../app/views/views.py", line 177, in test
vehicles = Vehicle.query.all()
File ".../lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 452, in __get__
mapper = orm.class_mapper(type)
File ".../lib/python3.5/site-packages/sqlalchemy/orm/base.py", line 421, in class_mapper
mapper = _inspect_mapped_class(class_, configure=configure)
File ".../lib/python3.5/site-packages/sqlalchemy/orm/base.py", line 400, in _inspect_mapped_class
mapper._configure_all()
File ".../lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 1167, in _configure_all
configure_mappers()
File ".../lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 2688, in configure_mappers
mapper._post_configure_properties()
File ".../lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 1710, in _post_configure_properties
prop.init()
File ".../lib/python3.5/site-packages/sqlalchemy/orm/interfaces.py", line 183, in init
self.do_init()
File ".../lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1617, in do_init
self._setup_join_conditions()
File ".../lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1692, in _setup_join_conditions
can_be_synced_fn=self._columns_are_mapped
File ".../lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1960, in __init__
self._determine_joins()
File ".../lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 2043, in _determine_joins
consider_as_foreign_keys=consider_as_foreign_keys
File "<string>", line 2, in join_condition
File ".../lib/python3.5/site-packages/sqlalchemy/sql/selectable.py", line 824, in _join_condition
a, a_subset, b, consider_as_foreign_keys)
File ".../lib/python3.5/site-packages/sqlalchemy/sql/selectable.py", line 881, in _joincond_scan_left_right
if nrte.table_name == b.name:
AttributeError: 'Join' object has no attribute 'name'
堆棧跟蹤2
Traceback (most recent call last):
File ".../lib/python3.5/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File ".../lib/python3.5/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File ".../lib/python3.5/site-packages/flask_restful/__init__.py", line 270, in error_router
return original_handler(e)
File ".../lib/python3.5/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File ".../lib/python3.5/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File ".../lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File ".../lib/python3.5/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File ".../lib/python3.5/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File ".../lib/python3.5/site-packages/flask_restful/__init__.py", line 270, in error_router
return original_handler(e)
File ".../lib/python3.5/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File ".../lib/python3.5/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File ".../lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File ".../lib/python3.5/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File ".../lib/python3.5/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File ".../app/views/views.py", line 177, in test
vehicles = TrackingIncidentType.query.all()
File ".../lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 452, in __get__
mapper = orm.class_mapper(type)
File ".../lib/python3.5/site-packages/sqlalchemy/orm/base.py", line 421, in class_mapper
mapper = _inspect_mapped_class(class_, configure=configure)
File ".../lib/python3.5/site-packages/sqlalchemy/orm/base.py", line 400, in _inspect_mapped_class
mapper._configure_all()
File ".../lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 1167, in _configure_all
configure_mappers()
File ".../lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 2685, in configure_mappers
raise e
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Original exception was: 'Join' object has no attribute 'name'
模型
class Asset(db.Model):
__tablename__ = 'assets'
asset_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
asset_type_id = db.Column(db.ForeignKey('asset_types.asset_type_id'))
...
__mapper_args__ = {
'polymorphic_on': asset_type_id
}
class Equipment(Asset):
__tablename__ = 'equipment'
asset_id = db.Column(db.ForeignKey('assets.asset_id'), primary_key=True)
...
...
__mapper_args__ = {
'polymorphic_identity':1
}
class Vehicle(Asset):
__tablename__ = 'vehicles'
asset_id = db.Column(db.ForeignKey('assets.asset_id'), primary_key=True)
...
...
__mapper_args__ = {
'polymorphic_identity':2
}
種資產類型
db=# select * from asset_types;
asset_type_id | name
---------------+-----------
1 | equipment
2 | vehicle
(2 rows)
我已經添加了第二堆棧跟蹤我有相關的錯誤,但我被你的問題混淆。第一個堆棧跟蹤中的AttributeError不是拋出的異常嗎? – mam8cc
對不起,堆棧跟蹤結束時出現了一條空白行,我認爲這是結束 - 我沒有看到最後幾行,包括'AttributeError'。我的錯。 –