Using migrations is the most recommended way if you are in production. To perform a migration you simply need to write a file that exports an object with two functions:
- up: transforms the current state of the models.
- down: reverses some specific functionality.
Note: This file must be in the migrations directory in the root of the project.
These functions receive as an parameter an object of type QueryInterface
and an instance of Sequelize. To make any kind of change you are interested in doing it in the up function.
To change a column type, just use the changeColumn
method:
module.exports = {
up(queryInterface, sequelize) {
return queryInterface.changeColumn('nombreModelo', 'nombreAtributo', {
type: sequelize.<nuevo tipo>,
// otros cambios
});
},
};
Finally you must use the CLI to run your migration:
Note: Install the package sequelize-cli if you still do not have it. p>
sequelize db:migrate
When the process is finished, your column must already be migrated. As a final recommendation, consult the documentation .