Migrations Add column after specified column column and index

I want to add a column through migrations after a specific column also indexes it (regular mysql index is not a unique index). Is this possible through secelize.js through migration. If so, how and if there are no alternatives for this through migration.

Can we fulfill a custom request to continue the migration.

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'); } }; 
+6
source share
2 answers

You can pass after or before to options to add a column after a specific 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'); } }; 
+13
source

As far as I know, there is no way to do this using the addColumn function.

But you can do this with a raw SQL query:

 ALTER TABLE UserDetails CHANGE COLUMN user_id user_id VARCHAR(50) AFTER some_column; 

You can run your own SQL queries in migrations like this:

 module.exports = { up: function(queryInterface, Sequelize) { return queryInterface.sequelize.query("ALTER TABLE UserDetails CHANGE COLUMN user_id user_id VARCHAR(50) AFTER some_column;") }, down: function(queryInterface, Sequelize) { return true; } }; 

Since you are already using native SQL, you can add a new column through raw SQL and place it after the column of your choice:

 ALTER TABLE UserDetails ADD COLUMN user_id VARCHAR(50) AFTER some_column; 
+1
source

All Articles