迁移后如何向模式添加新属性?
How to add new attributes to schema after the migration?
目前我正在做一个node.js项目,我在构建模式时发现了一个问题,通常,我使用http://docs.sequelizejs.com/manual/tutorial/migrations.html to define my schema, which is $ node_modules/.bin/sequelize model:generate --name User --attributes firstName:string,lastName:string,email:string
, and after the $ node_modules/.bin/sequelize db:migrate
, I can write these attributes into database. However, I am wondering how to add new attribute to schema after the migration, I searched and found this https://github.com/sequelize/cli/issues/133提供的命令行来讨论这个问题,但是之后我再次尝试了解决方案和 运行 $ node_modules/.bin/sequelize db:migrate
,它没有将新属性写入原始模式,我不明白问题出在哪里,下面是我的代码,我正在尝试添加两个属性'address'& 'height' 进入用户模式,你们能给我一些建议吗?谢谢!
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
let migration = [];
migrations.push(queryInterface.addColumn(
'address',
'height',
{
type: Sequelize.STRING,
}
));
return Promise.all(migrations);
return queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
firstName: {
type: Sequelize.STRING,
},
lastName: {
type: Sequelize.STRING,
},
email: {
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Users');
}
};
这个问题已经解决了,你可以用下面的link to solve that issue, also you can check the query part of sequelize to see the documents http://docs.sequelizejs.com/class/lib/query-interface.js~QueryInterface.html,真正的答案应该是
module.exports = {
up: function (queryInterface, Sequelize) {
return [
queryInterface.addColumn(
'users',
'height',
{
type: Sequelize.STRING,
}
),
queryInterface.addColumn(
'users',
'address',
{
type: Sequelize.STRING,
}
)
];
},
down: function (queryInterface, Sequelize) {
return [
queryInterface.removeColumn('users', 'height'),
queryInterface.removeColumn('users', 'address')
];
}
};
然后你可以输入
sequelize migration:create --name add-height-and-address-to-user
sequelize db:migrate
将新属性迁移到您的模型中。
目前我正在做一个node.js项目,我在构建模式时发现了一个问题,通常,我使用http://docs.sequelizejs.com/manual/tutorial/migrations.html to define my schema, which is $ node_modules/.bin/sequelize model:generate --name User --attributes firstName:string,lastName:string,email:string
, and after the $ node_modules/.bin/sequelize db:migrate
, I can write these attributes into database. However, I am wondering how to add new attribute to schema after the migration, I searched and found this https://github.com/sequelize/cli/issues/133提供的命令行来讨论这个问题,但是之后我再次尝试了解决方案和 运行 $ node_modules/.bin/sequelize db:migrate
,它没有将新属性写入原始模式,我不明白问题出在哪里,下面是我的代码,我正在尝试添加两个属性'address'& 'height' 进入用户模式,你们能给我一些建议吗?谢谢!
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
let migration = [];
migrations.push(queryInterface.addColumn(
'address',
'height',
{
type: Sequelize.STRING,
}
));
return Promise.all(migrations);
return queryInterface.createTable('Users', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER,
},
firstName: {
type: Sequelize.STRING,
},
lastName: {
type: Sequelize.STRING,
},
email: {
type: Sequelize.STRING,
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
},
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Users');
}
};
这个问题已经解决了,你可以用下面的link
module.exports = {
up: function (queryInterface, Sequelize) {
return [
queryInterface.addColumn(
'users',
'height',
{
type: Sequelize.STRING,
}
),
queryInterface.addColumn(
'users',
'address',
{
type: Sequelize.STRING,
}
)
];
},
down: function (queryInterface, Sequelize) {
return [
queryInterface.removeColumn('users', 'height'),
queryInterface.removeColumn('users', 'address')
];
}
};
然后你可以输入
sequelize migration:create --name add-height-and-address-to-user
sequelize db:migrate
将新属性迁移到您的模型中。