如何在更新Postgres中更新JSONB-COLUMN时访问行ID
继续问题如何使用knexjs,bookshelfjs 更新JSONB列,我想更新jsob-property,而不仅仅是字符串 但是作为[字符串 +当前行ID列值](第二个答案示例)。
return knex("tablename").update({
jsonbkey: knex.raw(`
jsonb_set(jsonbkey, '{city}','"Ayodhya ${ROW_ID_COLUMN_HERE}"')
`)
}).where({"id" :2020})
是否可以在一个查询期间进行此操作?
我请求的第二部分 - 如何仅对具有“城市”不像空字符串的行进行此查询。
return knex("tablename").update({
jsonbkey: knex.raw(`
jsonb_set(jsonbkey, '{city}','"Ayodhya ${ROW_ID_COLUMN_HERE}"')
`)
}).where({"id" :2020}).and.whereNot(jsonbkey.city, '')
我的行看起来像:
id | jsonbkey
1 | {"city": "", code: "EU"}
2 | {"city": "Paris", code: "FR"}
我一直在考虑两个查询,例如Array_agg,仅用于ID,然后循环抛出此数组 - 这是理性的吗?
Continuing the question how to update JSONB column using knexjs, bookshelfjs, I would like to update jsob-property not as just a string
but as [string + current row id column value] (the second answer example).
return knex("tablename").update({
jsonbkey: knex.raw(`
jsonb_set(jsonbkey, '{city}','"Ayodhya ${ROW_ID_COLUMN_HERE}"')
`)
}).where({"id" :2020})
Is it possible to do this operation during one query?
The second part of my request - how can I perform this query only for row that has those 'city' not as empty string.
return knex("tablename").update({
jsonbkey: knex.raw(`
jsonb_set(jsonbkey, '{city}','"Ayodhya ${ROW_ID_COLUMN_HERE}"')
`)
}).where({"id" :2020}).and.whereNot(jsonbkey.city, '')
My rows look like:
id | jsonbkey
1 | {"city": "", code: "EU"}
2 | {"city": "Paris", code: "FR"}
I have been thinking about two queries like array_agg for only ids and then loop throw this array - is it rational?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论