我正在尝试在3个模型之间建立角色基础关系,
UserModel:
class UserModel(db.Model):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
username = Column(String(120), unique=True, nullable=False)
password = Column(String(120), nullable=False)
reset_token = Column(String(120), nullable=True)
status = Column(Integer, nullable=True)
roleofuser = db.relationship('RoleUser', backref=db.backref('roleofuser'))
之后,我创建了角色和用户映射模型 UserRole模型
class RoleUser(db.Model):
__tablename__ = 'role_users'
id = Column(Integer(), primary_key=True)
role_id = Column(Integer(), db.ForeignKey('role.id'))
user_id = Column(Integer(), db.ForeignKey('users.id'))
role_rel = db.relationship('Role',
backref=db.backref('role_rel'))
user_rel = db.relationship('UserModel',
backref=db.backref('user_rel'))
最后是角色模型
class Role(db.Model):
__tablename__ = 'role'
id = Column(Integer(), primary_key=True)
name = Column(String(50), unique=True)
之后,我将使用棉花糖模式显示数据
class UserScheme(ma.MOdelSchema):
roleofuser = fields.Nested("RoleUserSchema")
username = fields.String(required=True)
class Meta:
model = UserModel
#fields = ("username","userrole")
""" Role USER mapped """
class RoleUserSchema(ma.Schema):
user_rel = fields.Nested("UserScheme")
role_rel = fields.Nested("RoleSchema")
class Meta:
model = RoleUser
"""Role """
class RoleSchema(ma.Schema):
roleuser_rel = fields.Nested("RoleUserSchema")
class Meta:
Role
现在问题出在调用user.query.all()
之后,
关系数据未附加在结果上,
得到像这样的结果:
{
roleofuser:{},
username: abcd123
}
我期待
{
roleofuser : {
role: 1,
name: 'admin'
},
username : 'admin232'
}