postgresql - 迁移发生后,Sequelize 模型定义文件会发生什么变化?

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

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

最佳答案

经过一段时间试图弄清楚这一点后,我与一位开发人员进行了 QA,他写了一篇关于 sequelize db 迁移 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).

关于postgresql - 迁移发生后,Sequelize 模型定义文件会发生什么变化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60174891/

相关文章:

sql - 为什么 GIN 三元组索引不与 ILIKE ANY 子句一起使用?

python - 在 Python : sqlalchemy. exc.programminingerror 语法错误处或附近执行原始 SQL 查询时出现问题

mysql - 如何使用 sequelize 用外键值填充表?

javascript - Sequelize promise 和正常 node.js 回调的问题

javascript - 从 Sequelize getter 方法访问关联表

cakephp - 从 Cake 1.3 迁移到 2.0 及更高版本 - 迁移现有的,还是仅用于新的?

ruby-on-rails - 未定义的方法 `spec' #<ActiveRecord::ConnectionAdapters::NullPool:0x00007fd44f8ab300> - Rails 6 schema:load

php - 在迁移 :refresh with --path option 时找不到 Laravel 5.5 迁移

java - Liquibase + Postgresql + Spring Jpa : Id auto increment issue

postgresql - ActiveRecord - 搜索具有多个值的 Postgres 数组?