我是烧瓶的新手,并尝试使用用户角色关联服务。我通过以下方式定义了三个数据库模型:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class user(db.Model):
id = db.Column(db.Integer, primary_key=True)
... some other fields
roles = db.relationship("Role", secondary='user_roles')
class Role(db.Model):
id = db.Column(db.Integer, primary_key=True)
role_name = db.Column(db.String(80), unique=True, nullable=False)
class UserRoles(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey("user.id", ondelete='CASCADE'), nullable=False)
role_id = db.Column(db.Integer, db.ForeignKey("role.id", ondelete='CASCADE'), nullable=False)
各自的模式是:
from flask_marshmallow import Marshmallow
ma = Marshmallow()
class UserSchema(ma.ModelSchema):
id = ma.Int(dump_only=True)
roles = ma.Nested(RoleSchema(only=("role_name",)))
class Meta:
model = User
sqla_session = db.session
class RoleSchema(ma.ModelSchema):
id = ma.Int(dump_only=True)
class Meta:
model = Role
sqla_session = db.session
class UserRoleSchema(ma.ModelSchema):
id = ma.Int(dump_only=True)
class Meta:
model = UserRoles
sqla_session = db.session
include_fk = True
当我尝试执行时,出现以下棉花糖验证错误:
{'roles': {'_schema': ['输入类型无效。']}}
我理解该消息,但由于缺乏知识,我找不到解决方案。非常感谢任何帮助。