Deploy flask application on Heroku with flask-sqlalchemy

Asked

Viewed 286 times

1

I have a small application written in flask and I am having problems with the database when trying to deploy in Heroku and after searching for two days I did not find any solution that suits me.

In short, this is the code of my app:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:1234@localhost/jperfil'
db = SQLAlchemy(app)
app.secret_key = '123456789'

#aqui vão os registros de algumas blueprints e imports de classes

db.create_all()

I can imagine that this is not how you configure a bank to deploy, because these are the settings of the local bank, but how to configure so that Heroku understands the settings and create my bank automatically?

  • I removed the <code>db.create_all()</code>, but still the error.

  • I don’t know what the deployment system is like in Heroku but in GCI (Google Cloud Infrastructure) a deploy file is used that what it does is create the database, so in Heroku you must have to create the database manually.

  • You can use an environment variable to avoid having to edit the URI database before deploying.

1 answer

0

You need to add the database add-on to your Heroku application (I confess I don’t understand much of it, but following this tutorial here https://devcenter.heroku.com/articles/getting-started-with-python#Provision-a-database you will manage to unwind).

Once configured this step, the only thing you should worry about in your application is properly starting the database through the command create_all (you already do) and set the variable

SQLALCHEMY_DATABASE_URI = os.environ['DATABASE_URL']

This way your application will take from the environment variable the connection string to the database provided by Heroku and already started in your application.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.