简体   繁体   中英

Sequelize db:migrate doesn't update model

I made a table using sequelize-cli, however I forgot to add a column: title.

So I generated new migration:

$ sequelize migration:create --name update-notes

And put these codes inside of migration file:

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {

    return queryInterface.addColumn(
      'Notes',
      'title',
      Sequelize.STRING
    );
  },

  down: (queryInterface, Sequelize) => {
   return queryInterface.removeColumn(
     'Notes',
     'title'
   );
  }
};

After run migration and check the table schema from DB, it works:

$ sequelize db:migrate

However model doesn't have my new added column yet:

'use strict';
// models/notes.js
module.exports = (sequelize, DataTypes) => {
  const Notes = sequelize.define('Notes', {
    content: DataTypes.TEXT    // NO 'TITLE' COLUMN
  }, {});
  Notes.associate = function(models) {
    // associations can be defined here
  };
  return Notes;
};

What am I missing? How should I apply my updates to model file?

Also what if previous data in table is not compatible with new table schema? Is it just fail the migration and developer fix the issue manually?

Unfortunately, with Sequelize migrations, you will need to manually update both your model and migration files. Creating a new migration file will not update your model file, and vice versa. Though the Sequelize CLI offers a few helper options, you're generally on your own to ensure your model file and migration file are defined correctly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM