Flask-migrate 不会在 Heroku 中创建数据库
Flask-migrate doesn't create database in Heroku
问题:
我无法让我的 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 出于某种原因没有创建数据库文件。我在 manage.py
中尝试了 open('textfile.txt', 'w')
,它成功地创建了文件。
您不能在 Heroku 上使用 sqlite。那是因为它将数据库存储为文件,但文件系统是临时的,不在测功机之间共享。 heroku run
启动一个新的测功机,它只在命令的持续时间内持续。所以它在本地创建数据库,然后立即销毁包括新数据库在内的所有内容。
您需要使用 Postgresql 插件。
问题:
我无法让我的 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 出于某种原因没有创建数据库文件。我在 manage.py
中尝试了 open('textfile.txt', 'w')
,它成功地创建了文件。
您不能在 Heroku 上使用 sqlite。那是因为它将数据库存储为文件,但文件系统是临时的,不在测功机之间共享。 heroku run
启动一个新的测功机,它只在命令的持续时间内持续。所以它在本地创建数据库,然后立即销毁包括新数据库在内的所有内容。
您需要使用 Postgresql 插件。