This is my model:
class Person(Mixin, db.Model):
"""Person Table."""
__tablename__ = "person"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
faces = db.relationship('Face')
def __repr__(self):
return f"<Person {self.name}>"
This is the route:
from webargs import fields
from webargs.flaskparser import use_args
@main.route("/person", methods=['POST'])
@use_args({
"name": fields.Str(required=True)
})
def post_person(args):
person = Person(**args)
db.session.add(person)
db.session.commit()
# logger.info(f"id: ",person.id)
logger.info(f"Person: {person.to_dict()}")
return create_response(person.to_dict(),201)
Issue: If I execute this code, the logger and the create response will output a null id:
{
"message": "",
"result": {
"_id": null
},
"success": true
}
Expectation: Once we perform the commit, the object properties are available. I've confirmed this by printing/logging the id of the person using person.id, and it outputs the correct value. to_dict() outputs null instead of the correct value. It doesn't seem to have the committed object.
Interestingly enough if I uncomment the logger.info line, or if I add db.session.refresh(), the to_dict() outputs the correct result.
This is the result when I execute it with the line # logger.info(f"id: ",person.id) uncommented.
{
"message": "",
"result": {
"_id": 75,
"name": "Test"
},
"success": true
}
This is my model:
This is the route:
Issue: If I execute this code, the logger and the create response will output a null id:
{ "message": "", "result": { "_id": null }, "success": true }Expectation: Once we perform the commit, the object properties are available. I've confirmed this by printing/logging the id of the person using
person.id, and it outputs the correct value.to_dict()outputs null instead of the correct value. It doesn't seem to have the committed object.Interestingly enough if I uncomment the
logger.infoline, or if I add db.session.refresh(), theto_dict()outputs the correct result.This is the result when I execute it with the line
# logger.info(f"id: ",person.id)uncommented.{ "message": "", "result": { "_id": 75, "name": "Test" }, "success": true }