2016-03-25 67 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插件。