2016-02-18 77 views
1

我正在嘗試將一些模型重構爲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) 
+0

我已經添加了第二堆棧跟蹤我有相關的錯誤,但我被你的問題混淆。第一個堆棧跟蹤中的AttributeError不是拋出的異常嗎? – mam8cc

+1

對不起,堆棧跟蹤結束時出現了一條空白行,我認爲這是結束 - 我沒有看到最後幾行,包括'AttributeError'。我的錯。 –

回答

0

遺憾的是我沒能發佈所有與此相關的問題(上千款行)的代碼,但我會嘗試解釋的答案,無論...

我正在將兩個表格(車輛和設備)重構爲一個聯合繼承層次結構。重構的一部分是從表中刪除vehicles.vehicle_idequipment.equipment_id主鍵。因此,我必須更改我的模型中的所有外鍵列(即equipment_id = db.Column(db.ForeignKey('equipment.equipment_id')))。在將模型分解爲一個獨立的項目之後,我意識到在我的應用程序中出現了一個意想不到的模型,它並沒有被重構爲新的FK。這導致了神祕的錯誤。

所以一些代碼來希望清除此設置:重構之前

型號

class TrackingEvent(db.Model): 
    __tablename__ = 'tracking_events' 

    tracking_event_id = db.Column(db.Integer, primary_key=True) 
    equipment_id = db.Column(db.ForeignKey('equipment.equipment_id')) 

注:引用的equipment.equipment_id對模型或DB不再存在。 @KurtRaschke後

模型重構

class TrackingEvent(db.Model): 
    __tablename__ = 'tracking_events' 

    tracking_event_id = db.Column(db.Integer, primary_key=True) 
    asset_id = db.Column(db.ForeignKey('assets.asset_id'))