续集:在生产中更改模型架构

编程入门 行业动态 更新时间:2024-10-23 07:16:37
本文介绍了续集:在生产中更改模型架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们正在使用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:

CREATE TABLE IF NOT EXISTS

如果表存在,则实际上不更新架构.这是在其文档中指出的.

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:

docs.sequelizejs/manual/tutorial/migrations.html

这些功能使您可以在已知状态之间转换开发人员数据库,登台数据库和生产数据库.

These will enable you to transition developer, staging, and production databases between known states.

更多推荐

续集:在生产中更改模型架构

本文发布于:2023-11-16 07:39:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1602304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:续集   架构   模型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!