Python SqlAlchemy-AttributeError:mapper

Python SqlAlchemy-AttributeError:mapper,python,orm,sqlalchemy,attributeerror,falconframework,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

根据我的模型:

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)
回溯(最近一次呼叫最后一次):
文件“/home/ubuntu/.local/lib/python3.5/site packages/falcon/api.py”,第227行,在调用中__
响应程序(请求、响应,**参数)
文件“/app\u user/register.py”,第13行,在on\u post中
会话=会话(token='test-token-123')
文件“”,第2行,在_init中__
文件“/home/ubuntu/.local/lib/python3.5/site packages/sqlalchemy/orm/instrumentation.py”,第347行,处于“新”状态(如果没有)
state=self.\u state\u构造函数(实例,self)
文件“/home/ubuntu/.local/lib/python3.5/site packages/sqlalchemy/util/langhelpers.py”,第764行,在__
obj.\uuuuu dict\uuuuu[self.\uuuuuuuuuuu name\uuuuuuuuu]=结果=self.fget(obj)
文件“/home/ubuntu/.local/lib/python3.5/site packages/sqlalchemy/orm/instrumentation.py”,第177行,在构造函数中
self.dispatch.first_init(self,self.class_)
文件“/home/ubuntu/.local/lib/python3.5/site packages/sqlalchemy/event/attr.py”,第256行,在调用__
fn(*参数,**千瓦)
文件“/home/ubuntu/.local/lib/python3.5/site packages/sqlalchemy/orm/mapper.py”,第2976行,在第一次初始化时的事件中
配置映射器()
文件“/home/ubuntu/.local/lib/python3.5/site packages/sqlalchemy/orm/mapper.py”,第2872行,在configure\u mappers中
映射程序。_post_configure_properties()
文件“/home/ubuntu/.local/lib/python3.5/site-packages/sqlalchemy/orm/mapper.py”,第1765行,在“post\u-configure\u-properties”中
prop.init()
文件“/home/ubuntu/.local/lib/python3.5/site packages/sqlalchemy/orm/interfaces.py”,init中的第184行
self.do_init()
do_init中的文件“/home/ubuntu/.local/lib/python3.5/site packages/sqlalchemy/orm/relationships.py”,第1653行
self.\u进程\依赖\参数()
文件“/home/ubuntu/.local/lib/python3.5/site packages/sqlalchemy/orm/relationships.py”,第1710行,在进程相关参数中
self.target=self.mapper.mapped_表
文件“/home/ubuntu/.local/lib/python3.5/site packages/sqlalchemy/util/langhelpers.py”,第850行,在__
返回自我。\u回退\u getattr(键)
文件“/home/ubuntu/.local/lib/python3.5/site packages/sqlalchemy/util/langhelpers.py”,第828行,在_fallback\u getattr中
提升属性错误(键)
我不知道这个错误是从哪里来的,我不能真正调试它。。有人能帮我解决这个问题吗


谢谢和问候

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

  ...
  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可计算字符串作为字符串,其用法将在中进一步说明:

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

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


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

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

  ...
  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可计算字符串作为字符串,其用法将在中进一步说明:

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

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


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

错误来自用户关系配置,但原因尚不清楚
self.mapper
是解决给定问题的关系的一个记忆属性,但由于某些原因,对于AttributeError的用户来说,它失败了。我猜
model.user.user
.Related中有一些问题:我怀疑您在尝试实例化
会话
对象之前,没有在任何地方导入
model.user.user
类。换句话说,尽管类定义存在,但它尚未运行。例如,如果我将您的示例拆分为
model.base.base
model.session.session
,一个伪
model.user.user
,以及一个只导入
session
并尝试使用它的主函数,则会引发异常。另一方面,如果在使用模型之前导入了
会话
用户
s