迁移发生后,Sequelize 模型定义文件会怎样?
What happens to Sequelize Model definition files after a migration takes place?
通过网络阅读,不清楚您是否需要为迁移中创建的新列更新先前存在的模型中的模型定义,或者是否需要为迁移中创建的新表创建新的模型定义。一旦部署了模型定义文件,can/should 它会被更新吗?或者模型定义的更新是否只存在于迁移文件中?
在尝试解决这个问题一段时间后,我与一位开发人员进行了 QA,他写了一篇关于 sequelize 数据库迁移的精彩文章 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).
通过网络阅读,不清楚您是否需要为迁移中创建的新列更新先前存在的模型中的模型定义,或者是否需要为迁移中创建的新表创建新的模型定义。一旦部署了模型定义文件,can/should 它会被更新吗?或者模型定义的更新是否只存在于迁移文件中?
在尝试解决这个问题一段时间后,我与一位开发人员进行了 QA,他写了一篇关于 sequelize 数据库迁移的精彩文章 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).