How to Add, Delete new Columns in Sequelize CLI

后端 未结 7 1846
刺人心
刺人心 2020-12-07 16:43

I\'ve just started using Sequelize and Sequelize CLI

Since it\'s a development time, there are a frequent addition and deletion of columns. What the best the method

7条回答
  •  感情败类
    2020-12-07 17:11

    If you are using sequelize-cli you need to create the migration first. This is just a file that tells the engine how to update the database and how to roll back the changes in case something goes wrong. You should always commit this file to your repository

    $ sequelize migration:create --name name_of_your_migration
    

    The migration file would look like this:

    module.exports = {
      up: function(queryInterface, Sequelize) {
        // logic for transforming into the new state
        return queryInterface.addColumn(
          'Todo',
          'completed',
         Sequelize.BOOLEAN
        );
    
      },
    
      down: function(queryInterface, Sequelize) {
        // logic for reverting the changes
        return queryInterface.removeColumn(
          'Todo',
          'completed'
        );
      }
    }
    

    And then, run it:

    $ sequelize db:migrate
    

提交回复
热议问题