db.session.commit() not working inside instance method flask sqlalchemy

  flask-sqlalchemy, python, sqlalchemy

I have a model as below:

class Student(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    
    def change_name(self, new_name):
        self.name = new_name
        db.session.commit()

when I query student outside model

student = Student.query.filter_by(id=1).first()

and then update student name

student.change_name('good')

But the database does not change.

Source: Python Questions

LEAVE A COMMENT