问题描述
在postgres + sqlAlchemy设置中使用JSONB列时,我们经常使用.update()
方法。
一个例子是:
path = '{user,lastText}'
text = 'Hello. Please activate folder \nD:\\foo\bar\some\more \t Thanks,yours "Ted"'
db.session.query(
Customer
).filter(
Customer.custid == '10'
).update(
{Customer.context: func.jsonb_set(Customer.context,path,text)},synchronize_session='fetch'
)
您会看到文本中有很多有问题的字符,需要转义。
推荐这样做的方法是什么? sqlAlchemy应该有这样的模块吗?
解决方法
伊利亚(Ilja)的答案就在以下地方:
json.dumps()
这是一个非常好的方法。