2016-08-12 75 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 
    
相關問題