我想通过运行本教程中使用的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