2016-08-18 31 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')); 
    }, 
};