首页 > 解决方案 > 为什么我会收到“CommandError:App 'app_name' 没有迁移。” 使用 Heroku 时?

问题描述

所以我已经将我的 Django 项目部署到 Heroku,现在尝试迁移数据库。我在本地服务器上一切正常。然后我尝试在heroku中运行命令,如下所示。
heroku run python manage.py makemigrations app_name'.
这工作得很好。

Migrations for 'app_name':
  contents\migrations\0001_initial.py
    - Create model Book   
    - Create model Category
    - Create model Content

然后当然,我试过了:heroku run python manage.py migrate app_name
但是后来我收到了这个错误CommandError: App 'app_name' does not have migrations.

,我已经对可能的问题进行了一些研究,但这些都与我的无关。
例如,我确实__init__.pymigrationsapp_name 目录内的文件夹中。另外,我也试过heroku run python manage.py migrateheroku run python manage.py migrate app_name
我很困惑。你认为是什么问题?提前致谢。:)

标签: pythondjangoheroku

解决方案


As explained in heroku's help article Why are my file uploads missing/deleted?:

The Heroku filesystem is ephemeral - that means that any changes to the filesystem whilst the dyno is running only last until that dyno is shut down or restarted.

That is you do generate the migration files but they cease to exist shortly after when the app is reloaded.

In general the strategy you take is not a good approach to migrations as this can cause various issues, let's say you have multiple production servers each generates their own migrations this causes a conflict! In your situation suppose you do migrate this way and then later you want to add some new models this again will cause you problems.

One should always add their migrations to the version control (git, etc.) commit them and push them to production (here your heroku dyno). That is run python manage.py makemigrations locally, commit the generated migrations, push them and then run heroku run python manage.py migrate.


推荐阅读