How to auto generate migrations with Sequelize CLI from Sequelize models? How to auto generate migrations with Sequelize CLI from Sequelize models? node.js node.js

How to auto generate migrations with Sequelize CLI from Sequelize models?


If you don't want to recreate your model from scratch, you can manually generate a migration file using the following CLI command:

sequelize migration:generate --name [name_of_your_migration]

This will generate a blank skeleton migration file. While it doesn't copy your model structure over to the file, I do find it easier and cleaner than regenerating everything. Note: make sure to run the command from the containing directory of your migrations directory; otherwise the CLI will generate a new migration dir for you


You cannot create migration scripts for existing models.

Resources:

If going the classic way, you'll have to recreate the models via the CLI:

sequelize model:create --name MyUser --attributes first_name:string,last_name:string,bio:text

It will generate these files:

models/myuser.js:

"use strict";module.exports = function(sequelize, DataTypes) {  var MyUser = sequelize.define("MyUser", {    first_name: DataTypes.STRING,    last_name: DataTypes.STRING,    bio: DataTypes.TEXT  }, {    classMethods: {      associate: function(models) {        // associations can be defined here      }    }  });  return MyUser;};