cphill
cphill

Reputation: 5924

Sequelize-CLI Add Column to Existing Model

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.

Upvotes: 4

Views: 8885

Answers (3)

Insan Jati
Insan Jati

Reputation: 61

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.

Upvotes: 2

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'));
    },
};

Upvotes: 4

Bhagya M
Bhagya M

Reputation: 265

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.

Upvotes: 10

Related Questions