I am new to flask and try to wtite a user role association service. I defined the three database models in a following way:
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)
The respective schemas are:
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
When I tried to execute I got the following marshmallow validation error:
{'roles': {'_schema': ['Invalid input type.']}}
I understood the message but due to a lack of knowledge, I could not find the solution. Any help is highly appreciated.