Search code examples
mysqlnode.jsmigrationsequelize.jssequelize-cli

How to add new attributes to schema after the migration?


currently I am working on a node.js project, and I found a problem while I was building the schema, usually, I use command line provided by http://docs.sequelizejs.com/manual/tutorial/migrations.html to define my schema, which is $ node_modules/.bin/sequelize model:generate --name User --attributes firstName:string,lastName:string,email:string , and after the $ node_modules/.bin/sequelize db:migrate, I can write these attributes into database. However, I am wondering how to add new attribute to schema after the migration, I searched and found this https://github.com/sequelize/cli/issues/133 is discussing this problem, but after I tried the solution and run $ node_modules/.bin/sequelize db:migrate again, it did not write the new attributes to the original schema, I don't understand where's the problem, below is my code, I am trying to add two attributes 'address'& 'height' into the user schema, can you guys give me some advice? Thank you!

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    let migration = [];
    migrations.push(queryInterface.addColumn(
            'address',
            'height',
            {
                type: Sequelize.STRING,
              }
        ));

    return Promise.all(migrations);
    return queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      firstName: {
        type: Sequelize.STRING,
      },
      lastName: {
        type: Sequelize.STRING,
      },
      email: {
        type: Sequelize.STRING,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
    });
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Users');
  }
};

Solution

  • this issue has been solved, you can use below link How to Add, Delete new Columns in Sequelize CLI to solve that issue, also you can check the query part of sequelize to see the documents http://docs.sequelizejs.com/class/lib/query-interface.js~QueryInterface.html, The real answer should be

    module.exports = {
      up: function (queryInterface, Sequelize) {
        return [
          queryInterface.addColumn(
            'users',
            'height',
            {
              type: Sequelize.STRING,
            }
          ),
          queryInterface.addColumn(
            'users',
            'address',
            {
              type: Sequelize.STRING,
            }
          )
        ];
      },
    
      down: function (queryInterface, Sequelize) {
        return [
          queryInterface.removeColumn('users', 'height'),
          queryInterface.removeColumn('users', 'address')
        ];
      }
    };
    

    and then you can type

    sequelize migration:create --name add-height-and-address-to-user
    sequelize db:migrate
    

    to migrate new attributes into your model.