javascript - Sequelize db:migrate doesn't update model - Stack Overflow

admin2025-04-08  1

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 patible with new table schema? Is it just fail the migration and developer fix the issue manually?

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 patible with new table schema? Is it just fail the migration and developer fix the issue manually?

Share Improve this question asked Oct 21, 2018 at 15:24 modernatormodernator 4,41914 gold badges49 silver badges77 bronze badges 3
  • 1 With Sequelize migrations, you will need to manually update both your model and migration files. Running a new migration will not automatically update your model file. – mcranston18 Commented Oct 21, 2018 at 19:35
  • @mcranston18 Oh, that's bad news
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744124804a232415.html

最新回复(0)