13

I want to execute a raw query in my migrations up and down functions.

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
  }

};
danielrvt
  • 10,177
  • 20
  • 80
  • 121

1 Answers1

33

The query() method you are looking for is an instance rather than class method. It exists on Sequelize instances, not on the class itself.

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(...);
  }

};
Timshel
  • 1,653
  • 12
  • 9