本文介绍了Sequelize迁移是否应该更新模型文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sequelize迁移是否可以使模型文​​件与数据库保持一致?

Are Sequelize migrations supposed to keep your model files in line with your database?

我使用了sequelize cli引导一个简单的项目并创建了一个模型node_modules/.bin/sequelize model:generate --name User --attributes email:string.我完全没有问题地进行了迁移.

I used the sequelize cli to bootstrap a simple project and create a model node_modules/.bin/sequelize model:generate --name User --attributes email:string. I migrated this with no issue.

然后,我创建了以下迁移文件,以向用户电子邮件属性添加notNull约束.

Then I created the following migration file to add a notNull constraint to the user email attribute.

更新电子邮件迁移

const models = require("../models")

module.exports = {
  up: (queryInterface, Sequelize) => {
      return queryInterface.changeColumn(models.User.tableName, 'email',{
        type: Sequelize.STRING,
        allowNull: false,
      });
    },

  down: (queryInterface, Sequelize) => {
      return queryInterface.changeColumn(models.User.tableName, 'email',{
        type: Sequelize.STRING,
      });
    },
};

数据库架构已更新为添加约束,但模型文件未添加.进行迁移时,是否可以自动更新模型文件?

The database schema updated to add the constraint but the model file did not. Is there a way to automatically update the model files as you make migrations?

推荐答案

不幸的是,没有.除了从sequelize model:create最初创建之外,sequelize模型和迁移之间没有同步级别.但是,此问题建议的工作流程是,更新应将迁移传播到模型.这使我有些困惑,因为通常情况下,迁移是根据对模型的更改自动生成的,而反之则不一定.

Unfortunately, no. There is no level of synchronization between sequelize models and migration except for their initial creation from sequelize model:create. However, the workflow suggested in this question is that updates should propagate from migrations to models. This confuses me slightly, as typically the migrations are to be auto-generated from changes to the models, not necessarily vice-versa.

跟踪这些功能的持续开放问题可能对您有所帮助:

There have been on-going open issues tracking these features that you may find helpful to subscribe to:

  • Generate initial migration from model?
  • Auto-Create Migrations
  • Possible to generate migrations to update existing models?

社区建议/解决方案:

请参阅 https://stackoverflow.com/a/28431648/8954866 如建议的那样,一种解决方法是在引入简单更改时使用sequelize-cli重新创建模型.但是,这种方法存在明显的局限性,因为无法从cli中定义许多配置(例如关联).

Refer to https://stackoverflow.com/a/28431648/8954866As suggested, a workaround is to re-create models using sequelize-cli when introducing simple changes. However, there are clear limitations to this methodology as many configurations are not possible to define from the cli such as associations.

此外,确实存在一个npm软件包 sequelize-auto-migrations 确实存在为生成迁移提供支持,但是这似乎不是一个过于活跃的项目.

Additionally, an npm package sequelize-auto-migrations does exist that does provide support for generating migrations, however it doesn't seem to be an overly active project.

结论:

Sequelize/sequelize-cli尚不支持此功能.

Sequelize/sequelize-cli does not yet support this feature.

ORM是否应该创建工具来自动生成迁移?是的,这是理想的功能,否则开发人员将被迫违反诸如 DRY 之类的原则,并在不同文件中管理相同的逻辑.

Should an ORMs create tools to auto generate migrations?Yes, this is desired functionality as otherwise the developers are forced to violate principles such as DRY and manage the same logic in different files.

这篇关于Sequelize迁移是否应该更新模型文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 05:39
查看更多