Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Sequelize migrations update model files?

Are Sequelize migrations supposed to keep your model files in line with your database?

I used the sequelize cli to bootstrap a simple project and create a model node_modules/.bin/sequelize model:generate --name User --attributes email:string. I migrated this with no issue.

Then I created the following migration file to add a notNull constraint to the user email attribute.

updateEmail migration

const models = require("../models")

module.exports = {
  up: (queryInterface, Sequelize) => {
      return queryInterface.changeColumn(models.User.tableName, 'email',{
        type: Sequelize.STRING,
        allowNull: false,
      });
    },

  down: (queryInterface, Sequelize) => {
      return queryInterface.changeColumn(models.User.tableName, 'email',{
        type: Sequelize.STRING,
      });
    },
};

The database schema updated to add the constraint but the model file did not. Is there a way to automatically update the model files as you make migrations?

like image 239
Jeffrey Kandel Avatar asked Mar 07 '18 20:03

Jeffrey Kandel


People also ask

What is up and down in Sequelize migration?

If you mean what's up and what's down: - up: all commands will be executed when running sequelize db:migrate - down: all commands will be executed when running sequelize db:migrate:undo. Sequelize also says the development environment is default, but I experienced problems with this.


1 Answers

Is there a way to automatically update the model files as you make migrations?

Unfortunately, no. There is no level of synchronization between sequelize models and migration except for their initial creation from sequelize model:create. However, the workflow suggested in this question is that updates should propagate from migrations to models. This confuses me slightly, as typically the migrations are to be auto-generated from changes to the models, not necessarily vice-versa.

There have been on-going open issues tracking these features that you may find helpful to subscribe to:

  • Generate initial migration from model?
  • Auto-Create Migrations
  • Possible to generate migrations to update existing models?

Community Suggestions/Solutions:

Refer to https://stackoverflow.com/a/28431648/8954866 As suggested, a workaround is to re-create models using sequelize-cli when introducing simple changes. However, there are clear limitations to this methodology as many configurations are not possible to define from the cli such as associations.

Additionally, an npm package sequelize-auto-migrations does exist that does provide support for generating migrations, however it doesn't seem to be an overly active project.

Conclusion:

Should Sequelize migrations update model files?

Sequelize/sequelize-cli does not yet support this feature.

Should an ORMs create tools to auto generate migrations? Yes, this is desired functionality as otherwise the developers are forced to violate principles such as DRY and manage the same logic in different files.

like image 69
vapurrmaid Avatar answered Jan 06 '23 08:01

vapurrmaid