update state with flask sqlalchemy with postgres will not commit to database

徘徊边缘 提交于 2019-12-02 09:23:44

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.

def save(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.orm import session
...
session = session.object_session(update_this)
session.commit()

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!