如何使用queryInterface.bulkUpdate从Sequelize当有必要检查一个键是否存在于jsonb列?



我想使用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) => {
}
};

在失去一些头发后寻找答案。

我是这样解决的:

'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

我希望看到其他的方法来解决这个问题。

相关内容

最新更新