且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSON字段的更新不会持久保存到数据库

更新时间:2022-12-18 09:32:50

如果您使用的是Postgres< 9.4您不能直接更新JSON字段.您需要 flag_modified 函数将更改报告给SQLAlchemy:

If you are using Postgres < 9.4 you can't update JSON field directly. You need flag_modified function to report the change to SQLAlchemy:

from sqlalchemy.orm.attributes import flag_modified
model.data['key'] = 'New value'
flag_modified(model, "data")
session.add(model)
session.commit()