SQLAlchemy很多很多关系经典/映射

时间:2016-01-05 01:44:25

标签: python orm flask sqlalchemy relationship

我尝试使用mapper映射下面的类,但是当我尝试访问属性user.roles时,我有以下消息:

' NoneType'对象没有属性'角色'

该属性未被填充。

有人可以帮助我吗?

谢谢!

user_mapping = Table('user', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(50)),
    Column('email', String(100)),
    Column('password', String(100)),
    Column('password_hash',String(200))
)

role_user_mapping = Table('role_user', metadata,
    Column('role_id', Integer, ForeignKey('role.id')),
    Column('user_id', Integer, ForeignKey('user.id')),
    PrimaryKeyConstraint('role_id', 'user_id', name='role_user_pk')
)

role_mapping = Table('role', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(50)),
    Column('description', String(200)),
)

def init(db):

    mapper(User, user_mapping, properties={    

        'roles' : relationship('Role', secondary='role_user')
    })

    mapper(Role_User, role_user_mapping)


    mapper(Role, role_mapping, properties={

        'users' : relationship(User, secondary='role_user')

    })

0 个答案:

没有答案