问题:
我无法让我的 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
,它成功地创建了文件。