joshreesjones
joshreesjones

Reputation: 1954

Flask-migrate doesn't create database in Heroku

The problem:

I'm having trouble getting my SQLite database to work on Heroku. When I try to go to the app's URL in my browser, I get an internal server error. heroku logs shows this:

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']

What I've tried:

I can reproduce this error locally by deleting my local database (there's nothing important in it yet):

$ 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']

I can fix it locally by using Flask-Migrate's upgrade command:

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

However, when I try to fix it on Heroku by doing the same thing, it doesn't work:

$ 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']

I've also tried manually dropping and creating the tables:

$ 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']

I'm pretty stumped at what to do. It looks like Flask-Migrate doesn't create the database file for some reason. I tried open('textfile.txt', 'w') in manage.py, and it successfully created the file.

Upvotes: 0

Views: 850

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

You can't use sqlite on Heroku. That's because it stores the db as a file, but the filesystem is ephemeral and not shared between dynos. heroku run spins up a new dyno which only lasts for the duration of the command. So it creates the db locally, and then immediately destroys everything including the new db.

You need to use the Postgresql add-on.

Upvotes: 2

Related Questions