如何在更新Postgres中更新JSONB-COLUMN时访问行ID

发布于 2025-02-13 18:39:38 字数 916 浏览 0 评论 0原文

继续问题如何使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文