本文介绍了无法在Sequelize迁移中执行原始查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Sequelize迁移来更新数据库,因此我试图编写这样的Sequelize迁移
I am trying to update my database using Sequelize migrations so I have tried to write a Sequelize migrations like this
'use strict';
module.exports = {
up: (queryInterface, Sequelize, migration) => {
queryInterface.addColumn('coaching_class_entries', 'bill_cycle', {
type: Sequelize.INTEGER(4),
allowNull: false,
defaultValue: '0',
field: 'bill_cycle',
after: 'fees'
})
.then(() => queryInterface.addColumn('coaching_classes', 'bill_plans', {
type: Sequelize.JSON,
allowNull: false,
defaultValue: 'NULL',
field: 'bill_plans',
after: 'bill_cycle'
}))
.then(() =>
migration.migrator.Sequelize.query('UPDATE coaching_classes SET bill_plans = JSON_ARRAY(JSON_OBJECT("cycle", bill_cycle, "fee", fees));'));
},
down: (queryInterface, Sequelize) => {
let migrations = [];
migrations.push(queryInterface.removeColumn('coaching_class_entries', 'bill_cycle'))
migrations.push(queryInterface.removeColumn('coaching_classes', 'bill_plans'))
return Promise.all(migrations);
}
};
但是它总是在原始查询行中给我错误
But it is always giving me error in raw query line
正确的语法是什么?
推荐答案
我自己发现它,只是我们必须使用简单的queryInterface.sequelize.query
I found it myself only we have to use simple queryInterface.sequelize.query
这篇关于无法在Sequelize迁移中执行原始查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!