I have reviewed at least 20 stackover flow answers, plus lots of blog posts, and I am at a complete loss for how to migrate my local postgres db to heroku for a simple flask app. I have been following along with several different tutorials and everything works locally. I can push the app to production on heroku and I don't get any errors during that process. Specifically, I am running the following in the terminal in succession:
python manage.py db init
python manage.py db migrate
python manage.py db upgrade
heroku create hud-mfi-api
git remote add prod https://git.heroku.com/hud-mfi-api.git
heroku config:set APP_SETTINGS=config.ProductionConfig --remote prod
heroku addons:create heroku-postgresql:hobby-dev --app hud-mfi-api
git push prod master
heroku run python manage.py db upgrade --app hud-mfi-api
When I then go to the app, I get psycopg2.errors.UndefinedTable
. Included in the error is a link to the SQLAlchemy documentation but I reviewed it extensively and I didn't find anything specific regarding UndefinedTable after migrating. I have very little experience in the Flask and SQL world and I am trying to learn by going through various tutorials.
One hypothesis I have is that I already have a database populated with values and in all of the tutorials they are setting up an empty database. I don't know why this would be an issue though if everything is working correctly on my local version (i.e. python manage.py runserver
)
Once I then deploy to heroku and try to run the same get request I get this response
I apologize in advance as I know questions like this are quite common. I tried to include as much relevant information as possible but at this point I am quite frustrated with the process. I've spent time going through the documentation but at this point I think there is some issue on the SQL side but I can't parse it. Thanks in advance SO
Here is the code for my app.py
import os
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object(os.environ['APP_SETTINGS'])
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
from models import MFI
@app.route("/getall")
def get_all():
try:
median_incomes = MFI.query.all()
return jsonify([e.serialize() for e in median_incomes])
except Exception as e:
return(str(e))
@app.route("/get/<geoid_>")
def get_by_geoid(geoid_):
try:
median_income = MFI.query.filter_by(geoid=geoid_).first()
return jsonify(median_income.serialize())
except Exception as e:
return(str(e))
if __name__ == '__main__':
app.run(debug=True)
Here is the code for my manage.py file
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import app, db
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
Here is my congig.py script
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
DEBUG = False
TESTING = False
CSRF_ENABLED = True
SECRET_KEY = 'this-really-needs-to-be-changed'
SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL']
class ProductionConfig(Config):
DEBUG = False
class StagingConfig(Config):
DEVELOPMENT = True
DEBUG = True
class DevelopmentConfig(Config):
DEVELOPMENT = True
DEBUG = True
class TestingConfig(Config):
TESTING = True