title: How to automatically create database tables and schema with Flask and Sqalchemy?
body: <p>I have a small Flask application. I add an extra security layer which is log in. I based my refactoring on the <a href="https://www.digitalocean.com/community/tutorials/how-to-add-authentication-to-your-app-with-flask-login" rel="nofollow noreferrer">DO article</a>.</p>
  
  <p>In a nutshell,</p>
  
  <p><code>__init__.py</code>:</p>
  
  <pre><code>from flask import Flask
  from flask_login import LoginManager
  from flask_sqlalchemy import SQLAlchemy
  from werkzeug.security import generate_password_hash
  from sqlalchemy import create_engine
  
  # init SQLAlchemy so we can use it later in our models
  db = SQLAlchemy()
  
  def create_app():
      app = Flask(__name__)
  
      app.config['SECRET_KEY'] = 'e56432f4402c9a0c166fe6c6a0a2fbbd'
      app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'
  
      db.init_app(app)
  
          # blueprint for auth routes in our app
      from .auth import auth as auth_blueprint
      app.register_blueprint(auth_blueprint)
  
      # blueprint for non-auth parts of app
      from .main import main as main_blueprint
      app.register_blueprint(main_blueprint)
  
      login_manager = LoginManager()
      login_manager.login_view = 'auth.login'
      login_manager.init_app(app)
  
      from .models import User
  
      @login_manager.user_loader
      def load_user(user_id):
          # since the user_id is just the primary key of our user table, use it in the query for the user
          return User.query.get(int(user_id))
  
      return app
  </code></pre>
  
  <p>In order to create DB, I need to run in REPL:</p>
  
  <pre><code>  from project import db, create_app
    db.create_all(app=create_app())
  </code></pre>
  
  <p>This is both inconvenient and makes Docker image creation harder. I run an application as <code>flask run</code>. I saw similar issues but don't understand how to apply to my scenario.</p>
  
  <p>How can I overcome this complication?</p>
  
  <p>UPDATE 1:</p>
  
  <p>My project structure is the following:</p>
  
  <pre><code>project/
    project/
      __init__.py
      auth.py
      main.py
      models.py
      static/
      templates/
    Dockerfile
    requirements.txt
  </code></pre>