【问题标题】:How to use queryInterface.bulkUpdate from Sequelize when it is necessary to check if a key is present in a jsonb column?当需要检查 jsonb 列中是否存在键时,如何使用 Sequelize 中的 queryInterface.bulkUpdate?
【发布时间】:2021-10-29 05:26:53
【问题描述】:

我想使用 Sequelize 迁移来更新我的 Postgres 数据库中的表。

我的 SQL 查询使用来自 Postgres 的 de ? 运算符作为 JSONB 列。

我需要运行的 SQL 查询是:

UPDATE messages SET type = 'products' WHERE type = 'gallery' and content ? '%textMain%'

我在 Sequelize 中的迁移如下所示:

'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.bulkUpdate(
      'messages', 
      { type: 'products' }, 
      // HERE GOES THE WHERE CLAUSE
    )
  },

  down: async (_queryInterface, _Sequelize) => {
  }
};

【问题讨论】:

    标签: node.js postgresql sequelize.js


    【解决方案1】:

    脱发后寻找答案。

    我解决了这个问题:

    'use strict';
    
    module.exports = {
      up: async (queryInterface, Sequelize) => {
        await queryInterface.bulkUpdate(
          'messages', 
          { type: 'products' }, 
          Sequelize.literal("type = 'gallery' AND content ? 'textMain'")
        )
      },
    
      down: async (_queryInterface, _Sequelize) => {
      }
    };
    

    希望它可以帮助其他有同样问题的人! :+1

    我很想看看解决这个问题的其他方法。

    【讨论】:

      猜你喜欢
      • 2018-07-26
      • 1970-01-01
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 2014-09-25
      • 2019-11-04
      • 1970-01-01
      相关资源
      最近更新 更多