1

I am working on a simple rest api using flask-sqlalchemy and flask-marshmallow.

For the PUT method I have below data:-

{ "childlist" : [  
  {  
    "childfield1" : "Value1",  --> field of related table      
    "childfield2" : "Value2"   --> field of related table  
  }  
 ],  
 "parentprimarykey1": "VAL1", --> this is the foreign key for child table  
 "parentfield2" : "VAL2",  
 "parentfield3" : "VAL3"  
}

When I pass this json to a PUT method and run the schema.load(request_data). SQLAlchemy is runs a Update SQL for the child table trying to update the foreign key with NULL.

Why an update statement is fired to update the foreign key? What is the best way to implement the update for the REST API?

my schema for the model looks like this Parent

class Parent(db.Model):
    all fields
    childlist=db.relationship('Parent', backref=db.backref('parent', cascade='delete, all', lazy='joined'))


class ParentSchema(ma.ModelScema):
    childlist=ma.Nested(child, many=True)
    class Meta:
        model=Parent
        sqla_session=db.session

the error is raised by this line in the view:- parent_schema.load(request_data)

  • I had to pass the primary key for the related table for the update to work properly. Used the solution from below discussion:- [link](https://stackoverflow.com/questions/31891676/update-row-sqlalchemy-with-data-from-marshmallow) – hitesh kumar Yadav Aug 02 '18 at 19:58

0 Answers0