Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 684
cphill Avatar asked Dec 06 '22 17:12

cphill


1 Answers

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.

like image 120
Bhagya M Avatar answered Feb 25 '23 23:02

Bhagya M