2017-08-06 31 views
0

基於我的模型:Python的SQLAlchemy的 - AttributeError的:映射

from sqlalchemy.ext.declarative import declarative_base 
from sqlalchemy import Column, Integer, String, ForeignKey 
from sqlalchemy.orm import relationship 

Base = declarative_base() 

class Session(Base): 
    __tablename__ = 'sessions' 

    id = Column(Integer, primary_key=True) 
    token = Column(String(200)) 
    user_id = Column(Integer, ForeignKey('app_users.id')) 
    user = relationship('model.user.User', back_populates='sessions') 

我想通過實例化一個新的會話:

session = Session(token='test-token-123') 

,但我得到:

AttributeError: mapper 

全stacktrace:

Traceback (most recent call last): 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/falcon/api.py", line 227, in __call__ 
    responder(req, resp, **params) 
    File "./app_user/register.py", line 13, in on_post 
    session = Session(token='test-token-123') 
    File "<string>", line 2, in __init__ 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/instrumentation.py", line 347, in _new_state_if_none 
    state = self._state_constructor(instance, self) 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py", line 764, in __get__ 
    obj.__dict__[self.__name__] = result = self.fget(obj) 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/instrumentation.py", line 177, in _state_constructor 
    self.dispatch.first_init(self, self.class_) 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/event/attr.py", line 256, in __call__ 
    fn(*args, **kw) 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 2976, in _event_on_first_init 
    configure_mappers() 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 2872, in configure_mappers 
    mapper._post_configure_properties() 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/mapper.py", line 1765, in _post_configure_properties 
    prop.init() 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/interfaces.py", line 184, in init 
    self.do_init() 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1653, in do_init 
    self._process_dependent_arguments() 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1710, in _process_dependent_arguments 
    self.target = self.mapper.mapped_table 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py", line 850, in __getattr__ 
    return self._fallback_getattr(key) 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py", line 828, in _fallback_getattr 
    raise AttributeError(key) 

我不知道這個錯誤來自哪裏,我不能真正調試它..任何人都可以幫助我解決這個問題?

感謝和問候!在回溯

+1

錯誤來自用戶關係配置,但爲什麼不清楚。 'self.mapper'是解析給定的[argument]關係的memoized屬性(http://docs.sqlalchemy.org/en/latest/orm/relationship_api.html#sqlalchemy.orm.relationship.params.argument) ,但出於某種原因,這對於您使用AttributeError失敗。我猜想[解決部分路徑]'model.user中存在一些問題(http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/relationships.html#declarative-configuring-relationships)。 User'。 –

+1

相關:https://stackoverflow.com/questions/44688346/vague-flask-sqlalchemy-error-attributeerror-mapper和https://stackoverflow.com/questions/33161280/sqlalchemy-instantiate-object-from-orm-fails -with-attributeerror-mapper –

+1

我懷疑你在嘗試實例化Session對象之前,沒有在任何地方導入過'model.user.User'類。換句話說,儘管類定義存在,但它尚未運行。例如,如果我將例子分割爲'model.base.Base','model.session.Session',一個虛擬'model.user.User'和一個主要函數**,它只導入'Session' **並嘗試使用它,引發異常。另一方面,如果在使用模型之前,您在某處導入了'Session'和'User',這樣就可以執行這兩個類定義。 –

回答

3

尋找你可以看到這幾行:

... 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1653, in do_init 
    self._process_dependent_arguments() 
    File "/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/relationships.py", line 1710, in _process_dependent_arguments 
    self.target = self.mapper.mapped_table 
    ... 

其縮小你的問題倒不少。關係

user = relationship('model.user.User', back_populates='sessions') 

使用Python的評估字符串作爲argument,它的使用在"Configuring Relationships"進一步解釋:

Relationships to other classes are done in the usual way, with the added feature that the class specified to relationship() may be a string name. The 「class registry」 associated with Base is used at mapper compilation time to resolve the name into the actual class object, which is expected to have been defined once the mapper configuration is used

如果您還沒有進口models.user模塊任何地方,你嘗試實例化一個前Session對象,則名稱解析失敗,因爲類User尚未創建並且不存在於註冊表中。換句話說,爲了解決名稱的工作問題,所有類都必須被定義,這意味着他們的身體必須已經被執行。

而且,如果您實際上已導入models.user模塊,請檢查您的其他模型,並確定其相關模型類已定義。首次使用您的模型觸發映射器編譯/配置,因此錯誤的來源也可能是其他型號