0

I have a very simple implementation of Flask, Flask-Restful, Flask-SqlAlchemy, and Flask-Marshmallow:

I am getting the validation error: {'_schema': ['Invalid input type.']} when trying to dump an object I just created.

init.py

app = Flask(__name__)
configure_rds(app)
db = SQLAlchemy(app)
ma = Marshmallow(app)
api = Api(app)
register_blueprints(app)

model.py

class MyModel(db.Model):
    __tablename__ = 'my_table'
    id = db.Column(db.Integer, primary_key=True)
    uid = db.Column(db.String(255))
    other_id = db.Column(db.Integer)
    last_modified = db.Column(db.Date)
    status = db.Column(db.String(255))

Schemas.py

class MyModelSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = MyModel
        load_instance = True

    id = ma.auto_field()
    uid = ma.auto_field()
    other_id = ma.auto_field()
    last_modified = ma.auto_field()
    status = ma.auto_field()

views.py

class MyModelView(Resource):   
  
    def get(self):
        maps = MyModel.query.all()
        ma_schema = MyModelSchema(many=True)
        body = ma_schema.dump(maps)
        resp = jsonify(body)
        resp.status_code = 200
        new_map = maps[0]
        return resp

    def post(self):
        args = self.parser.parse_args()
        ma_schema = MyModelSchema(many=False)
        # new_model = ms.load(args, session=db.session)
        # new_model .last_modified = date.today()
        new_model = MyModel(uid=args['uid'],
                                   other_id=args['other_id'],
                                   status=args['status'],
                                   last_modified=datetime.date.today())
        db.session.add(new_model )
        db.session.commit()
        body = ma_schema.dump(new_model)
        resp = jsonify(body)
        resp.status_code = 200

The get method works just fine. The post will add the new model to the database but then fail to serialize the model with the schema. It is also unable to load my reqparse args into the schema as well. Why would it be able to add to the RDS, read from the RDS, and yet not be able to serialize the object?

I have tried a bunch of different column types, tried various setups of the schema, nothing seems to work. Also I have verified that the exact types of the model's attributes in the get match the attributes of the model I am creating and attempting to dump... So I am really unsure of what else it could be.

  • 1
    When you commit your session the attributes are expired. You'll need to query your database again for the newly created record and pass the result into `schema.dump`. – PGHE Jul 14 '21 at 21:46
  • @PGHE Wouldnt that mean I could dump before committing then be good to go? I tried this and it still failed validation. – SGlascott Jul 15 '21 at 01:52
  • @PGHE I also just tried to implement the way you described and it still failed validation. – SGlascott Jul 15 '21 at 01:55
  • I completely removed the database component (the add and commit) and it still does not work. Even though I am loading then dumping again. – SGlascott Jul 15 '21 at 02:17
  • You're also using `SQLAlchemyAutoSchema` with `auto_field()`, which I don't think is correct. Try remove the lines with `auto_field()`, or use `SQLAlchemySchema`. – PGHE Jul 15 '21 at 02:19
  • So I removed the auto field. This still is not giving me a valid schema. I DID find out that I am not smart and just was not returning the response in my post method. returning the response works which means it actually is dumping it. But the question is, why is ma_schema.validate(mymodel) returning "invalid input"? – SGlascott Jul 15 '21 at 02:53
  • could you post the error ? – Bao Tran Jul 20 '21 at 03:12
  • @BaoTran it doesnt throw an error, just prints the above {'_schema': ['Invalid input type.']} – SGlascott Jul 26 '21 at 15:20
  • Try printing the new_model object .__dict__ to the console, see if any attribute is missing ( I suspect the id might be missing as @PGHE origanl comment stated) or is of the wrong type (usually wrong types are strings instead of numbers or vice versa) – Nir Sep 13 '21 at 06:54

0 Answers0