在迁移中执行原始查询 - Sequelize 3.30 [英] Execute raw query in migration - Sequelize 3.30

查看:16
本文介绍了在迁移中执行原始查询 - Sequelize 3.30的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的迁移 updown 函数中执行原始查询.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆