问题描述
我想在我的迁移 up
和 down
函数中执行原始查询.
I want to execute a raw query in my migrations up
and down
functions.
当我尝试这样做时:Sequelize.query
,它说 ERROR: Sequelize.query is not a function
.
When I try to do: Sequelize.query
, it says ERROR: Sequelize.query is not a function
.
这是我的迁移框架文件:
This is my migration skeleton file:
'use strict';
module.exports = {
up: (queryInterface, Sequelize, migration) => {
return Sequelize.query(...); //ERROR: Sequelize.query is not a Function
},
down: (queryInterface, Sequelize) => {
return Sequelize.query(...); //ERROR: Sequelize.query is not a Function
}
};
推荐答案
您要查找的 query()
方法是实例而不是类方法.它存在于 Sequelize
instances,而不是类本身.
The query()
method you are looking for is an instance rather than class method. It exists on Sequelize
instances, not on the class itself.
在迁移中,您可以通过提供的 queryInterface
对象作为 queryInterface.sequelize
访问实例.
In migrations, you can access the instance through the provided queryInterface
object as queryInterface.sequelize
.
所以你的迁移应该是这样的:
So your migration should look like:
'use strict';
module.exports = {
up: (queryInterface, Sequelize, migration) => {
return queryInterface.sequelize.query(...);
},
down: (queryInterface, Sequelize) => {
return queryInterface.sequelize.query(...);
}
};
这篇关于在迁移中执行原始查询 - Sequelize 3.30的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!