Heroku如何运行Python的manage.py migrate命令?

10

我在Heroku上部署了一个简单的Django应用

步骤:

- git push heroku master

- heroku run python manage.py makemigrations ( or + app_name)

看起来影响到:

  0002_main.py:
- Create model Comment
- Remove field status from match
- Remove field quantity from slot
- Add field avatar to account
- Add field slots to match
- Alter field verification_code on account
- Alter field verification_code on slot
- Add field match_object to comment
- Add field user to comment
- Alter index_together for comment (1 constraint(s))

然后我运行

- heroku run python manage.py migrate

但是我收到了:
  Running migrations:
  No migrations to apply.
  Your models have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
5个回答

20

请确保您已提交迁移文件。 然后运行

heroku run python manage.py migrate

您可以按照以下方式指定应用程序名称:

heroku run python manage.py migrate -a <app-name>
请查看此文档。

8

您的迁移文件应该提交到源代码控制中,不要在Heroku上运行makemigrations

有了提交的迁移文件,这个问题就不存在了。


1
没错,我错过了迁移文件,我只是在Heroku上运行了makemigrations。 - khue bui
但是如果我们想这样做呢?例如,如果我们已经修改了一个来自已安装包的应用程序模型,并且我们想在服务器上展示它,因为我们不发送包,只发送一个 requirements.txt 文件。 - Irfan wani

3

根据文档,Heroku文件系统是只读的。

这意味着,当您断开与dyno的连接时,makemigrations命令创建的文件将被销毁。

要解决此问题,请执行以下操作:

  1. 将您的迁移文件提交到Github(或源代码控制系统),然后在Heroku shell上运行migrate命令 - 推荐使用
  2. 创建迁移文件,然后在heroku bash shell上运行迁移。-生产环境下不推荐使用

2
为了让Heroku在部署时处理迁移,请将以下内容添加到您的Procfile中:
release: python manage.py migrate

1

执行下列命令:

heroku run python manage.py migrate

或者

heroku run python3 manage.py migrate

如果需要指定应用名称:

heroku run python3 manage.py migrate -a <app-name>


好的,尝试从ssh连接(直接在VM上)运行python manage.py migrate会导致5432端口错误,谢谢。 - Vitaliy Terziev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接