【问题标题】:What happens to Sequelize Model definition files after a migration takes place?迁移发生后 Sequelize 模型定义文件会发生什么?
【发布时间】:2020-05-27 05:12:32
【问题描述】:

通过网络阅读,不清楚您是否需要为迁移中创建的新列更新先前现有模型中的模型定义,或者为迁移中创建的新表创建新模型定义。部署模型定义文件后,可以/应该更新它吗?还是模型定义的更新只存在于迁移文件中?

【问题讨论】:

    标签: postgresql sequelize.js database-migration sequelize-cli


    【解决方案1】:

    经过一段时间试图弄清楚这一点,我与一位开发人员进行了 QA,他写了一篇关于 sequelize db migrations here 的精彩文章。我的问题和他的回答如下:

    问:One thing I don’t get is why you’re updating the model file as well as creating a migration file. I’m trying to update an existing, production db myself so I need to create a migration file. As far as I know, the model files I’ve created have been deployed and there is no use changing them now, the only way I can make an update to the db is only by way of migrations. So curious why you’re doing both here.

    答:The migration modifies columns of tables in the database itself. The model is merely the way to inform Sequelize which columns it can expect to be present in the database. Eg you can have a kenzo column on your database, but if you don’t specify that in the model, you wont be able to use it in the code. To try this out, you can add a field in the model without creating a migration for it, and then try to retrieve a record of that model. If the column is not added in the database, you will receive a column does not exist error. There’s no harm in removing fields from your model if they are still present in the database though (although I would recommend also removing them through a migration).

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-16
      • 2016-09-27
      • 2020-02-01
      • 2018-07-17
      • 1970-01-01
      • 2021-03-17
      • 1970-01-01
      • 2015-09-21
      相关资源
      最近更新 更多