Python SqlAlchemy - AttributeError:mapper

时间:2017-08-06 17:47:46

标签: python orm sqlalchemy attributeerror falconframework

基于我的模型:

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

完整的堆栈跟踪:

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 个答案:

答案 0 :(得分:4)

查看回溯,您可以看到以下这些行:

  ...
  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"进一步解释了其用法:

  

与其他类的关系以通常的方式完成,添加的特性是指定给relationship()的类可能是字符串名称。在映射器编译时使用与Base关联的“类注册表”将名称解析为实际的类对象,预计在使用映射器配置后定义

如果您在尝试首次实例化models.user对象之前未在任何位置导入Session模块,则名称解析将失败,因为类User尚未已创建但在注册表中不存在。换句话说,为了解析工作的名称,必须已经定义了所有类,这意味着它们的主体必须已被执行。

如果您确实导入了models.user模块,请检查其他模型并确定其相关的模型类。首次使用模型会触发映射器编译/配置,因此错误的来源也可能是其他模型