首页 新闻 会员 周边 捐助

python sqlalchemy many to many 问题

0
悬赏园豆:10 [待解决问题]

各位好:


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

无名少年的主页 无名少年 | 初学一级 | 园豆:192
提问于:2013-03-29 10:46
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册