如何使用 Sequelize CLI 从 Sequelize 模型自动生成迁移?

2021-11-20 00:00:00 node.js orm mysql sequelize.js

我有一组 Sequelize 模型.我想使用迁移,而不是数据库同步.

I have a set of Sequelize models. I want to use migrations, not DB Sync.

Sequelize CLI 似乎能够做到这一点,根据 这篇文章:当您使用 CLI 生成模型时,您还将免费获得迁移脚本."

Sequelize CLI seems to be able to do this, according to this article: "When you use the CLI for the model generation, you will gain the migration scripts for free as well."

如何使用 Sequelize CLI 从现有的 Sequelize 模型自动生成迁移?

How to auto generate the migrations with Sequelize CLI from existing Sequelize models?

推荐答案

如果您不想从头开始重新创建模型,可以使用以下 CLI 命令手动生成迁移文件:

If you don't want to recreate your model from scratch, you can manually generate a migration file using the following CLI command:

sequelize migration:generate --name [name_of_your_migration]

这将生成一个空白的骨架迁移文件.虽然它不会将您的模型结构复制到文件中,但我确实发现它比重新生成所有内容更容易、更干净.注意:确保从迁移目录的包含目录运行命令;否则 CLI 会为你生成一个新的迁移目录

This will generate a blank skeleton migration file. While it doesn't copy your model structure over to the file, I do find it easier and cleaner than regenerating everything. Note: make sure to run the command from the containing directory of your migrations directory; otherwise the CLI will generate a new migration dir for you

相关文章