Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrations Add Column after specified column and index column

I want to add column via migrations after a certain column also index it (Normal mysql index not unique index). Is this possible via sequelize.js via migrations. If yes how and if no any alternatives for this via migrations.

Can we execute custom query in sequelize migrations.

Below is my existing migration file.

'use strict';

module.exports = {
  up: function (queryInterface, Sequelize) {
    return queryInterface.addColumn('UserDetails', 'user_id', {
      type: Sequelize.INTEGER,
    });
  },

  down: function (queryInterface, Sequelize) {
    return queryInterface.removeColumn('UserDetails', 'user_id');
  }
};
like image 822
Sudesh Avatar asked Jul 29 '15 14:07

Sudesh


1 Answers

You can pass after or before in the options to add column after some specified column.

'use strict';

module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.addColumn('UserDetails', 'user_id', {
  type: Sequelize.INTEGER,
  after: "some_column"
});
},

down: function (queryInterface, Sequelize) {
return queryInterface.removeColumn('UserDetails', 'user_id');
}
};

P.S. after option is only supported by MySQL https://github.com/sequelize/sequelize/blob/master/lib/query-interface.js#L495

like image 177
Vivek V Dwivedi Avatar answered Oct 19 '22 10:10

Vivek V Dwivedi