问题:
我无法让我的SQLite数据库在Heroku上运行。当我尝试在浏览器中访问应用程序的URL时,出现内部服务器错误。 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由于某种原因没有创建数据库文件。我在open('textfile.txt', 'w')
中尝试了manage.py
,并成功创建了该文件。
答案 0 :(得分:2)
你不能在Heroku上使用sqlite。那是因为它将数据库存储为文件,但文件系统是短暂的,不会在dynos之间共享。 heroku run
旋转一个新的dyno,它只持续命令的持续时间。因此它在本地创建数据库,然后立即销毁包括新数据库在内的所有内容。
您需要使用Postgresql插件。