postgresql Sequelize:在生产中更改模型架构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17708620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sequelize: Changing model schema on production
提问by Matt
We're using the orm sequelize.jsand have defined a model as such:
我们正在使用 orm sequelize.js并定义了一个模型:
module.exports = function(sequelize, DataTypes) {
var Source = sequelize.define('Source', {
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
}
}, {
paranoid: true
});
return Source;
};
This is deployed to production and sync'd to the database using sequelize.sync
. Next step, we add a parameter:
这被部署到生产环境并使用sequelize.sync
. 下一步,我们添加一个参数:
module.exports = function(sequelize, DataTypes) {
var Source = sequelize.define('Source', {
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
},
location: {
type: DataTypes.STRING
}
}, {
paranoid: true
});
return Source;
};
However, when deploying to production sequelize.sync
does not add this new parameter. This is because sync
does a:
但是,在部署到生产时sequelize.sync
不会添加此新参数。这是因为sync
做了一个:
CREATE TABLE IF NOT EXISTS
CREATE TABLE IF NOT EXISTS
And does not actually update the schema if the table exists. This is noted in their documentation.
如果表存在,则不会实际更新架构。这在他们的文档中有说明。
The only option seems to be to { force: true }
, however this is not okay for a production database.
唯一的选择似乎是{ force: true }
,但是这对于生产数据库来说是不行的。
Does anyone know how to properly update the schema when changes are necessary?
有谁知道如何在需要更改时正确更新架构?
回答by Dan Kohn
You want to implement Sequelize migrations:
你想实现 Sequelize 迁移:
http://docs.sequelizejs.com/manual/tutorial/migrations.html
http://docs.sequelizejs.com/manual/tutorial/migrations.html
These will enable you to transition developer, staging, and production databases between known states.
这些将使您能够在已知状态之间转换开发人员、登台和生产数据库。
回答by Saro
A quicker way would be using {alter: true}
option.
一种更快的方法是使用{alter: true}
选项。
Ref: https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-method-sync
参考:https: //sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-method-sync