Что происходит с файлами определений Sequelize Model после миграции? - PullRequest
0 голосов
/ 11 февраля 2020

При чтении через Интернет неясно, нужно ли вам обновлять определения моделей в ранее существующих моделях для новых столбцов, созданных в процессе миграции, или создавать новые определения моделей для новых таблиц, созданных в процессе миграции. После того, как файл определения модели был развернут, можно ли его обновить? Или обновления определений моделей существуют только в файлах миграции?

1 Ответ

0 голосов
/ 11 февраля 2020

Через некоторое время, пытаясь это выяснить, у меня был QA с разработчиком, который написал отличную статью о сиквелизировании миграций БД здесь . Мой вопрос и его ответ следующие:

Q: 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.

A: 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).

...