Sequelize: Changing model schema on production(Sequelize:在生产中更改模型架构)
问题描述
我们正在使用 orm sequelize.js 并定义了这样的模型:
We're using the orm sequelize.js and have defined a model as such:
module.exports = function(sequelize, DataTypes) {
var Source = sequelize.define('Source', {
name: {
type: DataTypes.STRING,
allowNull: false,
unique: true
}
}, {
paranoid: true
});
return Source;
};
这被部署到生产环境并使用 sequelize.sync 同步到数据库.下一步,我们添加一个参数:
This is deployed to production and sync'd to the database using sequelize.sync. Next step, we add a parameter:
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;
};
但是,当部署到生产环境时,sequelize.sync 不会添加这个新参数.这是因为 sync 做了一个:
However, when deploying to production sequelize.sync does not add this new parameter. This is because sync does a:
如果不存在则创建表
如果表存在,则实际上不会更新架构.这是在他们的文档中注明的.
And does not actually update the schema if the table exists. This is noted in their documentation.
唯一的选择似乎是 { force: true },但这对于生产数据库来说是不行的.
The only option seems to be to { force: true }, however this is not okay for a production database.
有谁知道在需要更改时如何正确更新架构?
Does anyone know how to properly update the schema when changes are necessary?
推荐答案
你想实现 Sequelize 迁移:
You want to implement Sequelize migrations:
http://docs.sequelizejs.com/manual/tutorial/migrations.html
这些将使您能够在已知状态之间转换开发人员、暂存和生产数据库.
These will enable you to transition developer, staging, and production databases between known states.
这篇关于Sequelize:在生产中更改模型架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Sequelize:在生产中更改模型架构
基础教程推荐
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
