title: Getting flask-sqlalchemy to create tables from Models with db.create_all
body: <p>I have a Docker setup, with a Flask app and a Postgresql backend. I want to be able to create all of my tables in Postgres from their Models defined in Flask-SqlAlchemy. This is currently not working for me.</p>
  
  <p>In my app __init__.py I create my app with config files.</p>
  
  <pre><code>from flask import Flask
  
  
  def create_app():
      app = Flask(__name__)
      app.config.from_pyfile("config.py")
      app.config.from_pyfile("secrets.py")
      return app
  
  
  app = create_app()
  
  
  from app import routes
  </code></pre>
  
  <p>In my routes.py I make the DB from the app, and then set it up before the first request(with my Models imported):</p>
  
  <pre><code>from .models import User, Community, Post, Debate
  
  ...
  
  db = SQLAlchemy(app)
  
  
  @app.before_first_request
  def setup():
      db.init_app(app)
      with app.app_context():
          db.metadata.drop_all(bind=db.engine)
          db.metadata.create_all(bind=db.engine)
          db.session.commit()
  </code></pre>
  
  <p>My models inherit from a BaseModel I made which inherits from db.Model, where db is an import of the db I defined in routes.py.</p>
  
  <p>What am I doing wrong here? How can I make flask-sqlalchemy automatically create my tables for me? Am I barking up the wrong tree?</p>