4

I have been reading through a good amount of the sequelize-cli documentation and can't seem to figure out how to add a column to an existing model that I have in place. I have a model called, models/team.js and want to add a column named role_name to the model with sequelize model:create --name team --attributes role_name:string, but I receive a message to overwrite rather then modify:

Loaded configuration file "config/config.json".
Using environment "development".
The file /Users/user/Desktop/Projects/node/app-repo/app/models/team.js already exists. Run "sequelize model:create --force" to overwrite it.

I don't want to overwrite the file that makes me think that this isn't the right command to use. It also makes me wonder if this is not possible from the cli and must be adjusted at the migration file level.

cphill
  • 5,596
  • 16
  • 89
  • 182

3 Answers3

10

You need a another migration file for adding the column.

In the up function you need to add

 queryInterface.addColumn(
     'nameOfAnExistingTable',
      'nameofTheNewAttribute',
      Sequelize.STRING)

In the down function you need to

queryInterface.removeColumn(
      'nameOfAnExistingTable',
      'nameOfTheAttribute')

Then run the migration.

Steve Horn
  • 8,818
  • 11
  • 45
  • 60
Bhagya M
  • 255
  • 5
  • 7
  • How can I generate another migration file with command as we have command like this 'node_modules/.bin/sequelize model:generate --name User --attributes firstName:string' This will generate Model and migration file at the same time. – Ketav Jun 11 '18 at 12:51
  • 2
    And as you suggest, I do the same but Model file is not updated with new fields in migration file. – Ketav Jun 11 '18 at 12:54
  • @KetavChotaliya did you ever figure this out? I just ran my first migration, but my models file was not updated :/ - Is the expectation that you manually update the models file with each migration? – jamis0n Jan 08 '20 at 00:07
4

In case you want to do multiple changes in single migration, you can just return chained promises like this:

module.exports = {
up: function (queryInterface, Sequelize) {
    return queryInterface.addColumn('yourTableName', 'firstNewColumnName', Sequelize.STRING)
        .then(_ => queryInterface.addColumn('yourTableName', 'secondNewColumnName', Sequelize.STRING))
        .then(_ => queryInterface.addColumn('yourTableName', 'thirdNewColumnName', Sequelize.STRING));
    },

down: function (queryInterface, _Sequelize) {
    return queryInterface.removeColumn('yourTableName', 'firstNewColumnName')
        .then(_ => queryInterface.removeColumn('yourTableName', 'secondNewColumnName'))
        .then(_ => queryInterface.removeColumn('yourTableName', 'thirdNewColumnName'));
    },
};
2

I think I got exactly same issue with you. I tried to find a sequelize command that will generate me a migration file with queryInterface.addColumn within. But eventually I understood that it can't happen in just one command. We need to create the migration file first.

Try to run sequelize command to see all the available options. You'll know that if you run sequelize model:generate (just the same as model:create) you'll make a new files of model and migration at the same time. But you want the migration file, but you don't want the model file. It's not the right command.

So if you just want to add column, all you need to do is use the sequelize migration:generate command to create the migration file. If you try to run that command, you will also see another options, and please notice that there's an option named --name which described as required. Just give a descriptive name as your need. Example command: sequelize migration:generate --name add-registration-complete-and-currency-column.

Finally, modify your migration file using QueryInterface and add the column in the model file manually. I hope it helps.

Insan Jati
  • 61
  • 5