sqlalchemy+mysql+json列更新不工作

7cwmlq89  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(403)

我在我的项目中使用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'
slhcrj9b

slhcrj9b1#

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'

相关问题