Skip to content Skip to sidebar Skip to footer

Update State With Flask Sqlalchemy With Postgres Will Not Commit To Database

I have read quite a bit of documentation and I can't see what is wrong with these lines update_this = User.query.filter_by(email=email).first() update_this.emailconfirmed = True db

Solution 1:

Ideally you want to maintain a single session throughout your application lifecycle. This way it makes it easy to reason about and you avoid binding sessions to individual models.

Thanks @Ilja Everila

In main.py instead of initializing SQLAlchemy you should write,

db.init_app(app)

Define a save instance method for your User model.

defsave(self):
    """Saves model object instance
    """
    db.session.add(self)
    db.session.commit()

You can call this method to save the instance as

update_this.save()

Another way to update the entity is to get the specific object session before committing

from sqlachemy.ormimport session
...
session = session.object_session(update_this)
session.commit()

Post a Comment for "Update State With Flask Sqlalchemy With Postgres Will Not Commit To Database"