Python SqlAlchemy-AttributeError:映射器 [英] Python SqlAlchemy - AttributeError: mapper

查看:242
本文介绍了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')

我想通过以下方式实例化新会话:

I want to instantiate a new session through:

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)

我不知道此错误是从哪里来的,我无法真正调试它..有人可以帮助我解决这个问题吗?

I have no idea where this error is coming from and i can not really debug it.. anybody could help me with this issue?

感谢和问候!

推荐答案

看看回溯,您会看到以下几行:

Looking at the traceback you can see these lines:

  ...
  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
  ...

可以将您的问题缩小很多.关系

which narrow your problem down quite a bit. The relationship

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

使用Python可评估的字符串作为:

uses a Python evaluable string as the argument, the use of which is further explained in "Configuring Relationships":

与其他类的关系以通常的方式完成,其附加功能是指定给relationship()的类可以是字符串名称.与Base相关联的类注册表"在映射器编译时用于将名称解析为实际的类对象,,一旦使用了映射器配置,该对象有望被定义

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

如果您在第一次尝试实例化Session对象之前尚未导入models.user模块,则名称解析失败,因为尚未创建类User且该类不存在注册表.换句话说,要使名称解析正常工作,必须定义所有类,这意味着它们的主体必须已执行.

If you've not imported models.user module anywhere before you try to instantiate a Session object for the first time, then the name resolving fails because the class User has not been created yet and does not exist in the registry. In other words for the name resolving to work, all classes must have been defined, which means that their bodies must have been executed.

并且如果您实际上已经导入了models.user模块,请检查其他模型以及是否已定义了它们的相关模型类.首次使用您的模型会触发映射器的编译/配置,因此错误的根源也可能是其他模型.

And if you actually have imported the models.user module, check your other models and that their related model classes have been defined. Using your models for the first time triggers mapper compilation/configuration, so the source of the error could be other models as well.

这篇关于Python SqlAlchemy-AttributeError:映射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆