2016-03-25 86 views
0

創建數據庫的問題:燒瓶遷移不會在Heroku的

我無法讓我的SQLite數據庫在Heroku工作。當我嘗試在瀏覽器中訪問該應用的網址時,出現內部服務器錯誤。 heroku logs表明這一點:

OperationalError: (sqlite3.OperationalError) no such table: questions [SQL: u'SELECT questions.id AS questions_id, questions.title AS questions_title, questions.content AS questions_content \nFROM questions'] 

我已經試過:

我可以通過刪除我的本地數據庫本地重現此錯誤(沒有什麼重要的,它尚未):

$ rm data-dev.sqlite 
$ heroku local 
# Go to localhost:5000 in my browser, 500 Internal server error 
OperationalError: (sqlite3.OperationalError) no such table: questions [SQL: u'SELECT questions.id AS questions_id, questions.title AS questions_title, questions.content AS questions_content \nFROM questions'] 

我可以使用Flask-Migrate的upgrade命令在本地修復它:

$ python2 manage.py db upgrade 
$ heroku local 
# Go to localhost:5000, get a working website 

然而,當我嘗試做同樣的事情來修復它在Heroku,這是行不通的:

$ heroku run ls 
# Doesn't show any .sqlite database files 
$ heroku run python manage.py db upgrade 
# Go to the website URL, get 500 Internal server error 
$ heroku logs 
OperationalError: (sqlite3.OperationalError) no such table: questions [SQL: u'SELECT questions.id AS questions_id, questions.title AS questions_title, questions.content AS questions_content \nFROM questions'] 

我也嘗試手動刪除和創建表:

$ heroku run python manage.py shell 
>>> db.drop_all() 
>>> db.create_all() 
>>> quit() 
# Go to website URL, get 500 Internal server error 
$ heroku logs 
OperationalError: (sqlite3.OperationalError) no such table: questions [SQL: u'SELECT questions.id AS questions_id, questions.title AS questions_title, questions.content AS questions_content \nFROM questions'] 

我很害怕做什麼。由於某些原因,Flask-Migrate看起來不會創建數據庫文件。我試過manage.py中的open('textfile.txt', 'w'),併成功創建了該文件。

+0

你不能在Heroku上使用sqlite。你需要使用一個數據庫插件。 –

+0

這樣一個簡單的答案。我希望我早點知道。謝謝! – joshreesjones

回答

2

你不能在Heroku上使用sqlite。這是因爲它將數據庫存儲爲文件,但文件系統是短暫的,並且不在dynos之間共享。 heroku run旋轉了一個新的測功機,只持續命令的持續時間。所以它在本地創建數據庫,然後立即銷燬包括新數據庫在內的所有數據。

您需要使用Postgresql插件。