danielrvt
danielrvt

Reputation: 10926

Execute raw query in migration - Sequelize 3.30

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
  }

};

Upvotes: 16

Views: 15330

Answers (1)

Timshel
Timshel

Reputation: 1783

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

};

Upvotes: 38

Related Questions