Flask-SQLAlchemy + MySQL + JSON专栏。更新无效

问题描述 投票:1回答:1

我在我的项目中使用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'
mysql flask sqlalchemy flask-sqlalchemy
1个回答
0
投票
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'
© www.soinside.com 2019 - 2024. All rights reserved.