Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
  }

};
like image 220
danielrvt Avatar asked Dec 21 '25 20:12

danielrvt


1 Answers

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

};
like image 167
Timshel Avatar answered Dec 24 '25 09:12

Timshel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!