【问题标题】:Migrate databases from local django to heroku将数据库从本地 django 迁移到 heroku
【发布时间】:2023-03-05 04:16:01
【问题描述】:

我在 Django 中有一个应用程序,我部署在 heroku 中。 部署运行良好,但我的模型的数据库没有迁移。

部署后,我再次本地运行:

python manage.py makemigrations
python manage.py migrate

之后,我会这样做:

heroku run python manage.py makemigrations
heroku run python manage.py migrate

在我运行服务器并等待它运行良好之后:

heroku run python manage.py runserver

我有 3 个模型:

客户、类别、产品

produto 有一个 ForeignKeycategoria。所以,localy,我有 3 个数据库:

produtos_produtoprodutos_categoriacliente

我使用PostgreSQL 作为本地数据库和heroku。

但是在heroku中,我没有任何这样的数据库。

当我运行服务器时,在浏览器中我得到以下答案:

ProgrammingError at /

relation "produtos_categoria" does not exist
LINE 1: ...ia"."descricao", "produtos_categoria"."logo" FROM "produtos_...
                                                             ^

Request Method:     GET
Request URL:    http://redewebsite.herokuapp.com/
Django Version:     1.9.2
Exception Type:     ProgrammingError
Exception Value:    

relation "produtos_categoria" does not exist
LINE 1: ...ia"."descricao", "produtos_categoria"."logo" FROM "produtos_...
                                                             ^

Exception Location:     /app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py in execute, line 64
Python Executable:  /app/.heroku/python/bin/python
Python Version:     2.7.11
Python Path:    

['/app',
 '/app/.heroku/python/bin',
 '/app/.heroku/python/lib/python2.7/site-packages/setuptools-19.6-py2.7.egg',
 '/app/.heroku/python/lib/python2.7/site-packages/pip-8.0.2-py2.7.egg',
 '/app',
 '/app/.heroku/python/lib/python27.zip',
 '/app/.heroku/python/lib/python2.7',
 '/app/.heroku/python/lib/python2.7/plat-linux2',
 '/app/.heroku/python/lib/python2.7/lib-tk',
 '/app/.heroku/python/lib/python2.7/lib-old',
 '/app/.heroku/python/lib/python2.7/lib-dynload',
 '/app/.heroku/python/lib/python2.7/site-packages']

Server time:    Sex, 4 Mar 2016 17:50:43 +0000

【问题讨论】:

    标签: django database postgresql heroku migration


    【解决方案1】:

    您必须在本地运行 makemigrations,然后将生成的迁移文件提交到 git。 Heroku 将在您部署时自动运行。

    【讨论】:

    • 我现在明白了。我们需要在迁移文件上添加 git add 并在之前推送它们。
    猜你喜欢
    • 2015-09-11
    • 2015-09-12
    • 2015-09-10
    • 1970-01-01
    • 2014-10-16
    • 2015-04-23
    • 2022-07-25
    • 2015-03-21
    • 2023-03-12
    相关资源
    最近更新 更多