ORM: InvalidRequestError: One or more mappers failed to initialize

【报错信息】:

InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers.  Original exception was: Class object expected, got 'Table('sys_user', MetaData(bind=None), Column('id', Integer(), table=<sys_user>, primary_key=True, nullable=False), Column('type', Integer(), table=<sys_user>, default=ColumnDefault(0)), Column('status', Integer(), table=<sys_user>, default=ColumnDefault(0)), Column('username', String(length=32), table=<sys_user>), Column('password', String(length=32), table=<sys_user>), Column('token', String(length=128), table=<sys_user>), schema=None)'.

 

会出现以上报错的情况:

1、model的外键定义时,写错了

2、....

 



user_role=Table("user_role",Base.metadata,
        Column('role_id',Integer,ForeignKey('sys_role.id')),
        Column('user_id',Integer,ForeignKey('sys_user.id'))
        )


class SysRole(Base):
    __tablename__='sys_role'

    id=Column(Integer,primary_key=True)
    name=Column(String(64))
    description=Column(String(128))
    users=relationship('sys_user',secondary=user_role,backref=backref('roles',lazy='select'))

    def __init__(self,items):
        self.init(items)

    def init(self,items):
        self.name=items['name']
        self.description=items['description']



class User(Base):
    __tablename__ = 'sys_user'

    id = Column(Integer, primary_key=True)
    type = Column(Integer, index=True, default=0)
    status = Column(Integer, default=0)
    username = Column(String(32), index=True, unique=True)
    password = Column(String(32))
    token = Column(String(128), index=True)
   
    #roles=relationship("sys_role",secondary=user_role)

    def __init__(self, username, password):
        self.username = username
        self.password = password
        self.token = get_random_string()

    def __repr__(self):
        return '<database.User %s>' % self.username



以上是我的表关系,在部署的时候没问题,而且可能在数据库生成相关的表,但在运行的时候出现如下异常,不知该如何解决:

Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/tornado-2.4.1-py2.7.egg/tornado/web.py", line 1042, in _execute
        getattr(self, self.request.method.lower())(*args, **kwargs)
      File "/home/liangli/puzhi/app/handlers/handlers.py", line 51, in post
        self.create_user(User(username, password))
      File "<string>", line 2, in __init__
      File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/instrumentation.py", line 309, in _new_state_if_none
        state = self._state_constructor(instance, self)
      File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/util/langhelpers.py", line 494, in __get__
        obj.__dict__[self.__name__] = result = self.fget(obj)
      File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/instrumentation.py", line 157, in _state_constructor
        self.dispatch.first_init(self, self.class_)
      File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/event.py", line 377, in __call__
        fn(*args, **kw)
      File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/mapper.py", line 2347, in _event_on_first_init
        configure_mappers()
      File "/usr/local/lib/python2.7/dist-packages/SQLAlchemy-0.7.9-py2.7.egg/sqlalchemy/orm/mapper.py", line 2260, in configure_mappers
        raise e
    InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers.  Original exception was: Class object expected, got 'Table('sys_user', MetaData(bind=None), Column('id', Integer(), table=<sys_user>, primary_key=True, nullable=False), Column('type', Integer(), table=<sys_user>, default=ColumnDefault(0)), Column('status', Integer(), table=<sys_user>, default=ColumnDefault(0)), Column('username', String(length=32), table=<sys_user>), Column('password', String(length=32), table=<sys_user>), Column('token', String(length=128), table=<sys_user>), schema=None)'.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值