2016-08-12 74 views
0

在成功完成a Heroku Python app with a Postgres db的教程后,我根据现有代码在Heroku应用程序上创建了一个新模型,方法是复制并粘贴现有模型及其表格的函数和文件。 但是我很难让Heroku远程创建Postgres表。经过一些反复试验后,我发现下面的答案。如何使用Python迁移将Heroku Django应用程序的新模型表添加到远程Heroku Postgres?

设置:Heroku Python在本地和远程运行,Heroku Postgres只能远程运行。

回答

1

我想通过运行本教程中使用的heroku run python manage.py migrate,在远程Heroku Postgres数据库上实际创建我的新表。但它并没有起作用。我需要做的是设置一些Python文件,然后在最后运行该命令。

如果您也编辑模型,例如添加新字段,则此功能起作用。

我所添加的文件到基于this tutorial

Heroku tutorial's code这正是我所做的:

  1. hello/models.py

    ,加

    class Mytable(models.Model): 
        when = models.DateTimeField('date created', auto_now_add=True) 
    
  2. 然后让Python生成0002_mytable.py文件在我的本地hello/migrations中,通过在我的Mac上运行以下命令终端(这是记录到的Heroku并在virtualenv中):

    python manage.py makemigrations hello 
    

    ,我得到这样的响应:

    Migrations for 'hello': 
        0002_mytable.py: 
        - Create model Mytable 
    
  3. 这个新的迁移文件添加到我的远程Heroku的

    git add hello/migrations/0002_mytable.py 
    git commit -am "added new migration file" 
    git push heroku master 
    
  4. 让Heroku远程创建表Heroku Postgres

    heroku run python manage.py migrate 
    

    你应该看到

    Running python manage.py migrate on ⬢ your-heroku-url... up, run.1699 
    Operations to perform: 
        Apply all migrations: admin, contenttypes, hello, sessions, auth 
    Running migrations: 
        Rendering model states... DONE 
        Applying hello.0002_mytable... OK 
    
相关问题