【问题标题】:How heroku run python manage.py migrate?heroku 如何运行 python manage.py 迁移?
【发布时间】:2017-04-09 00:02:06
【问题描述】:

我在 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.

【问题讨论】:

    标签: python django heroku


    【解决方案1】:

    您的迁移文件应该提交到您的源代码管理,并且永远不要在 heroku 上运行 makemigrations

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

    【讨论】:

    • 没错,我错过了迁移文件,我只是在 heroku 上运行了 makemigrations
    • 但是如果我们想这样做怎么办。例如,如果我们更改了来自已安装包的应用程序模型,并且我们希望在服务器上显示它,因为我们不发送包,而只是发送 requirements.txt 文件
    【解决方案2】:

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

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

    要解决您的问题,您可以:

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

    【讨论】:

      【解决方案3】:

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

      heroku run python manage.py migrate
      

      您可以通过以下方式指定应用名称:

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

      请参阅此文档。

      【讨论】:

        【解决方案4】:

        要么

        heroku run python manage.py migrate

        heroku run python3 manage.py migrate

        如果需要指定应用名称:

        heroku run python3 manage.py migrate -a &lt;app-name&gt;

        【讨论】:

        • hm 不错,尝试从 ssh 连接(直接在虚拟机上)运行 python manage.py 迁移会导致 5432 端口错误,谢谢
        猜你喜欢
        • 2021-05-23
        • 2016-02-01
        • 2013-12-12
        • 1970-01-01
        • 2015-09-21
        • 1970-01-01
        • 2017-12-15
        • 2013-07-21
        • 2014-02-04
        相关资源
        最近更新 更多