Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to execute the raw query in Sequelize migrations

I am trying to update my database using Sequelize migrations so I have tried to write a Sequelize migrations like this

'use strict';
module.exports = {
  up: (queryInterface, Sequelize, migration) => {
    queryInterface.addColumn('coaching_class_entries', 'bill_cycle', {
      type: Sequelize.INTEGER(4),
      allowNull: false,
      defaultValue: '0',
      field: 'bill_cycle',
      after: 'fees'
    })
      .then(() => queryInterface.addColumn('coaching_classes', 'bill_plans', {
        type: Sequelize.JSON,
        allowNull: false,
        defaultValue: 'NULL',
        field: 'bill_plans',
        after: 'bill_cycle'
      }))
      .then(() =>
        migration.migrator.Sequelize.query('UPDATE coaching_classes  SET bill_plans = JSON_ARRAY(JSON_OBJECT("cycle", bill_cycle, "fee", fees));'));

  },

  down: (queryInterface, Sequelize) => {
    let migrations = [];

    migrations.push(queryInterface.removeColumn('coaching_class_entries', 'bill_cycle'))
    migrations.push(queryInterface.removeColumn('coaching_classes', 'bill_plans'))
    return Promise.all(migrations);

  }
};

But it is always giving me error in raw query line

Cannot read property 'Sequelize' of undefined

What is the correct syntax for this?

like image 564
Vikas Avatar asked Mar 13 '18 08:03

Vikas


People also ask

How do I run a raw query in Sequelize?

Sequelize instance comes with the query() method which you can use to run a raw query. The syntax of the method is as shown below: const [results, metadata] = await sequelize. query( "Your query here", { options } );

Where is the raw query in Sequelize?

As there are often use cases in which it is just easier to execute raw / already prepared SQL queries, you can use the sequelize.query method. By default the function will return two arguments - a results array, and an object containing metadata (such as amount of affected rows, etc).

What are raw queries?

On the other hand, RawQuery serves as an escape hatch where you can build your own SQL query at runtime but still use Room to convert it into objects. RawQuery methods must return a non-void type. If you want to execute a raw query that does not return any value, use RoomDatabase#query methods.


1 Answers

I found it myself only we have to use simple queryInterface.sequelize.query

like image 123
Vikas Avatar answered Nov 15 '22 23:11

Vikas