0

Here is my docker-compose file:

version: "3.7"
services:
  db:
    restart: always
    image: postgres:14-alpine
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: dinopedia
    volumes:
      - postgres_data:/var/lib/postgresql/postgres/data
    ports:
      - 5432:5432

  app:
    restart: always
    build:
      context: .
      dockerfile: Dockerfile
    command: gunicorn --bind 0.0.0.0:8000 manage:app
    # command: python3 app.py run --host=0.0.0.0 --port=8000
    environment:
      FLASK_APP: app.py
      FLASK_ENV: docker
      POSTGRES_HOST: db
      POSTGRES_PORT: 5432
      POSTGRES_DB: dinopedia
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
    ports:
      - 8000:8000
    volumes:
      - .:/app
    depends_on:
      - db

volumes:
  postgres_data:

Here is the log:

dinopedia-app-1  |   File "/usr/local/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
dinopedia-app-1  |     raise exception
dinopedia-app-1  |   File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 1819, in _execute_context
dinopedia-app-1  |     self.dialect.do_execute(
dinopedia-app-1  |   File "/usr/local/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 732, in do_execute
dinopedia-app-1  |     cursor.execute(statement, parameters)
dinopedia-app-1  | sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "colour" does not exist
dinopedia-app-1  | LINE 2: FROM colour
dinopedia-app-1  |              ^
dinopedia-app-1  | 
dinopedia-app-1  | [SQL: SELECT count(%(count_2)s) AS count_1 
dinopedia-app-1  | FROM colour]
dinopedia-app-1  | [parameters: {'count_2': '*'}]
dinopedia-app-1  | (Background on this error at: https://sqlalche.me/e/14/f405)

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) relation "colour" does not exist

But everything works fine with the other command:

command: python3 app.py run --host=0.0.0.0 --port=8000

What am I doing wrong?

dslackw
  • 83
  • 1
  • 7
  • 1
    Do you create the database & tables before doing the select, using "Create database..", "Create table.."? – Clau St Jun 07 '22 at 07:29
  • 1
    Looks similar to https://stackoverflow.com/questions/25439166/how-do-i-force-flask-sqlalchemy-to-create-a-table – Clau St Jun 07 '22 at 07:33
  • The database was created but not the tables, that happens when I use gunicorn. When I run the app.py script with `command: python3 app.py run --host=0.0.0.0 --port=8000` works normally. – dslackw Jun 07 '22 at 07:40

1 Answers1

0

I solved this issue with (app.py):

if __name__ == '__main__':
    app.app_context().push()
    db.create_all()
    app.run(host='0.0.0.0', port=8000)
else:
    # Creates the database and tables 
    # for the production environment    
    app.app_context().push()
    db.create_all()

I don't know if it is the right way, but it works.

dslackw
  • 83
  • 1
  • 7
  • 1
    Pretty sure that you can ditch the else, and it's better to wrap the main calls in a separate method – Clau St Jun 09 '22 at 08:22