gpt4 book ai didi

mysql - Flask-SQLAlchemy + MySQL + JSON 列。更新不工作

转载 作者:行者123 更新时间:2023-11-29 07:33:21 25 4
gpt4 key购买 nike

我在我的项目中使用 SQLAlchemy。当我在 sqlalchemy 模型中修改 JSON 列时,它根本没有变化。那是我的代码:

class Order(DB.Model):

uid = DB.Column(DB.Integer, primary_key=True, index=True, unique=True)
status_uid = DB.Column( DB.Integer, default=1 )
json_data = DB.Column( DB.JSON, nullable=True )

orderobj = Order(json_data{'key':'old value'})
DB.session.add( orderobj )

#That is work fine
DB.session.commit()


# Trying to update
orderobj = Order.query.filter_by( status_uid=1 ).first()
orderobj.json_data['key'] = 'new value'

print(orderobj.json_data['key']) # -> 'old value'

DB.session.commit()

print(orderobj.json_data['key']) # -> 'old value'

最佳答案

class Order(DB.Model):

uid = DB.Column(DB.Integer, primary_key=True, index=True, unique=True)
status_uid = DB.Column( DB.Integer, default=1 )
json_data = DB.Column( DB.JSON, nullable=True )

orderobj = Order(json_data{'key':'old value'})
DB.session.add( orderobj )

#That is work fine
DB.session.commit()


# Trying to update
orderobj = Order.query.filter_by( status_uid=1 ).first()

json_data = deepcopy(orderobj.json_data)
json_data['key'] = 'new value'

orderobj.json_data=json_data

print(orderobj.json_data['key']) # -> 'old value'

DB.session.commit()

print(orderobj.json_data['key']) # -> 'old value'

关于mysql - Flask-SQLAlchemy + MySQL + JSON 列。更新不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50043077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com