David Reke
David Reke

Reputation: 555

Basic Flask App Not Creating Table in Heroku Postgres Database

I have a very basic Flask app with a postgres database that I am making along with a tutorial. It works locally, and I have it deployed to heroku, but when I try to run the application I get an Internal Server Error.

When I run heroku logs --app I get the following error report (I have included only the part that I think is relevant, but I can share the rest if requestsed)

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "favquotes" does not exist
2021-07-19T17:56:57.596569+00:00 app[web.1]: LINE 2: FROM favquotes
2021-07-19T17:56:57.596570+00:00 app[web.1]: ^
2021-07-19T17:56:57.596570+00:00 app[web.1]:
2021-07-19T17:56:57.596570+00:00 app[web.1]: [SQL: SELECT favquotes.id AS favquotes_id, favquotes.author AS favquotes_author, favquotes.quote AS favquotes_quote
2021-07-19T17:56:57.596571+00:00 app[web.1]: FROM favquotes]
2021-07-19T17:56:57.596571+00:00 app[web.1]: (Background on this error at: https://sqlalche.me/e/14/f405)

After some research I discovered I could check my database by using the Dataclips dashboard on heroku. When I took a look there I saw that under the Schema Explorer I get a message saying:

No columns or tables found

At this point I believe, but could be wrong, that my main python file is not creating the database as it should. Even though it does work to create a postgres database on my local machine. My main py file reads as follows:

from flask import Flask , render_template , request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from dotenv import load_dotenv
import os



app = Flask(__name__)

load_dotenv()

app.config['PROPAGATE_EXCEPTIONS'] = True
app.config['SQLALCHEMY_DATABASE_URI']=os.environ.get('SQL_URI')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False


db=SQLAlchemy(app)

class Favquotes(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    author = db.Column(db.String(30))
    quote= db.Column(db.String(2000))

@app.route('/')
def index():
    result = Favquotes.query.all()
    return render_template('index.html', result=result)



@app.route('/quotes')
def quotes():
    return render_template('quotes.html')


@app.route('/process', methods = ['POST'])
def process():
    author = request.form['author']
    quote = request.form['quote']
    quotedata = Favquotes(author=author, quote=quote)
    db.session.add(quotedata)
    db.session.commit()
    return redirect(url_for('index'))

Am I likely correct that my python code is creating the database as expected? And if so, what could I do to fix this?

Upvotes: 0

Views: 658

Answers (2)

charchit
charchit

Reputation: 1512

You can execute these commands using terminal too.

In heroku click on more button (top right) . Go to run console and type python. This will open python terminal.

Execute these commands to create the database.

from main import app,db
with app.app_context():
    db.create_all()

Upvotes: 3

David Reke
David Reke

Reputation: 555

After some searching I found I this solution. I could add the following line to my python code to create my table within heroku.

with app.app_context():
    db.create_all()

Upvotes: 1

Related Questions