Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method .to_dict() in core does not output the up to date object #29

Open
nicolae-stroncea opened this issue Jan 16, 2021 · 0 comments
Open

Comments

@nicolae-stroncea
Copy link

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
}
@nicolae-stroncea nicolae-stroncea changed the title SQLAlchemy object not up to date when calling .to_dict() .to_dict() in core does not output the up to date object Jan 16, 2021
@nicolae-stroncea nicolae-stroncea changed the title .to_dict() in core does not output the up to date object Method .to_dict() in core does not output the up to date object Jan 16, 2021
@nicolae-stroncea nicolae-stroncea changed the title Method .to_dict() in core does not output the up to date object Method .to_dict() in core does not output the up to date object Jan 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant