我想通過運行本教程中使用的heroku run python manage.py migrate
,在遠程Heroku Postgres數據庫上實際創建我的新表。但它並沒有起作用。我需要做的是設置一些Python文件,然後在最後運行該命令。
如果您也編輯模型,例如添加新字段,則此功能起作用。
我所添加的文件到基於this tutorial
的Heroku tutorial's code這正是我所做的:
- 在hello/models.py
,加
class Mytable(models.Model):
when = models.DateTimeField('date created', auto_now_add=True)
然後讓Python生成0002_mytable.py
文件在我的本地hello/migrations
中,通過在我的Mac上運行以下命令終端(這是記錄到的Heroku並在virtualenv中):
python manage.py makemigrations hello
,我得到這樣的響應:
Migrations for 'hello':
0002_mytable.py:
- Create model Mytable
這個新的遷移文件添加到我的遠程Heroku的
git add hello/migrations/0002_mytable.py
git commit -am "added new migration file"
git push heroku master
讓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