Search code examples
pythonherokuflaskpeewee

Heroku: relation does not exist


I am using Python, Flask, and the Peewee ORM. My DATABASE_URL is set correctly.

I have tested the functionality on a local server and it works correctly. When I deploy to Heroku, it errors out on the table that Peewee creates in the if name == main section of my app.

The error is:

ProgrammingError: relation "connection" does not exist (connection 
is the name of my model/would-be table).

UPDATE: I fixed the problem by creating the table in the views.py file under the specific route. Is this necessary? I would prefer a cleaner way.


Solution

  • I've never used Heroku, but I'm guessing that Heroku is importing your app in some way that bypasses the if __name__ == "__main__" block which is only run when this module is run directly. You should try moving the logic from that if block into a before_first_request handler so that it will still be run, but only once (per worker / app instance)

    @app.before_first_request
    def initialize():
        app.logger.info("Creating the tables we need")
        ...