2016-06-30 44 views
3

我在本地構建了一個Django 1.9項目,其中sqlite3作爲我的默認數據庫。我有一個名爲Download一個應用程序,它定義了DownloadedSongsmodels.pyHeroku上的Django:關係不存在

models.py現在

from __future__ import unicode_literals 
from django.db import models 


class DownloadedSongs(models.Model): 
    song_name = models.CharField(max_length = 255) 
    song_artist = models.CharField(max_length = 255) 

    def __str__(self): 
     return self.song_name + ' - ' + self.song_artist 

,爲了部署我的本地項目中的Heroku,我添加以下行的底部我settings.py文件:

import dj_database_url 
DATABASES['default'] = dj_database_url.config() 

我的應用程序有幾個文本字段的形式,並在提交這種形式,得到的數據插入到表DownloadedSongs。現在,當我部署了我在Heroku上的項目,並試圖提交此表,我得到了以下錯誤:

Exception Type: ProgrammingError at /download/ 
Exception Value: relation "Download_downloadedsongs" does not exist 
LINE 1: INSERT INTO "Download_downloadedsongs" ("song_name", "song_a... 

這是我的requirements.txt文件看起來像:

beautifulsoup4==4.4.1 
cssselect==0.9.1 
dj-database-url==0.4.1 
dj-static==0.0.6 
Django==1.9 
django-toolbelt==0.0.1 
gunicorn==19.6.0 
lxml==3.6.0 
psycopg2==2.6.1 
requests==2.10.0 
static3==0.7.0 

另外,我曾嘗試運行以下命令以及:

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

但是,問題仍然存在。這裏似乎有什麼錯誤?

+0

你可以訪問數據庫來查看錶的名字嗎?這是否可能是因爲表格全部是小寫?或者你還需要運行syncdb命令? – Furbeenator

+0

'syndb'命令在Django 1.9中已棄用。然而,我確實已經確定我運行了'makemigrations'命令。 –

回答

1

As Heroku's dynos don't have a filesystem that persists across deploys, a file-based database like SQLite3 isn't going to be suitable. It's a great DB for development/quick prototypes, though. https://stackoverflow.com/a/31395988/784648

所以整個SQLite數據庫將被消滅之間展開,你應該移動到一個專用的數據庫,當你部署到Heroku的我覺得。我知道,heroku有一個免費的postgres數據庫層,如果你只是想部署到heroku,我會推薦它。

2

您不能通過heroku run運行makemigrations。您必須在本地運行,並將結果提交給git。然後,您可以部署該代碼並通過heroku run python manage.py migrate運行生成的遷移。

原因是heroku run每次都會新建一個新的文件系統,因此第二個命令運行時,第一個命令中生成的任何遷移都會丟失。但在任何情況下,遷移都是部分代碼,並且必須在版本控制中。

+0

我也試過。但是,這個問題仍然存在。 –

3

確保您的本地遷移文件夾和內容受git版本控制。

如果沒有,添加,提交&推動它們如下(假設你有下<MYAPP>一個遷移的文件夾,你的遠程Git被稱爲「Heroku的」):

git add <myapp>/migrations/* 
git commit -m "Fix Heroku deployment" 
git push heroku 

等待,直到推是成功,你會得到本地提示。

然後登錄到heroku並執行遷移&遷移。 要在一個執行環境中執行此操作,請不要啓動這些命令作爲單個的heroku命令,而是啓動bash shell並在其中執行兩個命令:(不要鍵入'〜$',這代表Heroku提示)

heroku run bash 
~$ ./manage.py makemigrations 
~$ ./manage.py migrate 
~$ exit 
+0

對於我在測試中遇到的問題,我需要登錄到shell才能進行遷移。我做了上面的步驟,但不是使用「./manage.py」我使用了「python manage.py」(使用./給了我一個權限錯誤)。謝謝。 – aleksk

相關問題