2016-08-18 26 views
1

我一直在阅读大量的sequelize-cli文档,似乎无法弄清楚如何将列添加到现有模型中。我有一个模型叫,models/team.js,想添加一个名为role_namesequelize model:create --name team --attributes role_name:string模型列,但我收到一条消息覆盖而不是修改:Sequelize-CLI将列添加到现有模型

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. 

我不希望覆盖,使文件我认为这不是正确的命令。这也让我怀疑这是不可能的,必须在迁移文件级别进行调整。

回答

5

您需要另一个迁移文件来添加列。

在上行功能,您需要添加

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

在你需要

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

然后运行迁移的升降功能。

如果你想要做单迁移多次更改
1

,你可以只返回链式承诺如下:

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