title: How to get Flask-SQLAlchemy to work with the Application Factory Pattern
body: <p>I want to set up an sqlite database using Flask-SQLAlchemy. I am getting an Operational error <code>(sqlite3.OperationalError) no such table</code>.</p>
  
  <p>This is for a Web app written with flask. I want to interact with the database using Flask-SQLAlchemy extension. I feel it may be something to do with the application context, but I am not sure. </p>
  
  <p>As you can see the application has one 'module' (the <code>auth</code> sub-package). The module is registered to the application via a blueprint. I define the model for the <code>Researcher</code> class in the <code>models.py</code> file of the <code>auth</code> sub-package. <code>app/__init__.py</code> holds the Application Factory and the database initialization. 
  Because I am just trying to get the basic functionality working, the only two views I have are defined in <code>app/auth/routes.py</code>. </p>
  
  <hr>
  
  <pre><code>.
  |-- app
  |   |-- __init__.py
  |   |-- auth
  |   |   |-- __init__.py
  |   |   |-- __pycache__
  |   |   |-- models.py
  |   |   `-- routes.py
  |   |-- static
  |   `-- templates
  |-- app.db
  |-- config.py
  |-- instance
  |   `-- config.py
  `-- run.py
  </code></pre>
  
  <h1>app/<strong>init</strong>.py</h1>
  
  <pre><code>from flask import Flask, render_template
  from flask_sqlalchemy import SQLAlchemy
  db = SQLAlchemy()
  def create_app():
      app = Flask(__name__)
      app.config.from_pyfile("../config.py")
  
      db.init_app(app)
      with app.app_context():
          db.create_all()
  
      from app.auth import auth
      app.register_blueprint(auth, url_prefix="/auth/")
  
      return app 
  </code></pre>
  
  <h1>app/auth/routes.py</h1>
  
  <pre><code>from flask import Flask
  from app import db
  from models import User
  from app.auth import auth
  
  @auth.route("/insert")
  def insert():
      #r = Reasearcher("Pat","Den", "Pat_Den@mail.com", "operator","Dr.","Mr", "08611111", "+353", "0001")
      u = User(1,"Elija")
      db.session.add(u)
      db.session.commit()
  
  @auth.route("/query")
  def query():
      us = users.query.all()
      s = ""
      for u in us:
          s += u.name
      return s
  </code></pre>
  
  <h1>app/auth/models.py</h1>
  
  <pre><code>from flask import Flask
  from flask_sqlalchemy import SQLAlchemy
  from app import db
  
  class User(db.Model):
      __tablename__ = "users"
  
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String)
  
      def __init__(self,ids, name):
          self.id = ids
          self.name = name
  </code></pre>
  
  <h1>app/auth/<strong>init</strong>.py</h1>
  
  <pre><code>from flask import Flask, Blueprint
  
  auth = Blueprint("auth", __name__)
  
  import routes
  import models
  </code></pre>
  
  <h1>config.py</h1>
  
  <pre><code>DEBUG = True
  
  import os
  BASE_DIR = os.path.abspath(os.path.dirname(__file__))
  
  SQLALCHEMY_DATABASE_URI = "sqlite:////" + os.path.join(BASE_DIR, "app.db")
  DATABASE_CONNECT_OPTIONS = {}
  THREADS_PER_PAGE = 2
  
  CSRF_ENABLED     = True
  
  CSRF_SESSION_KEY = "secret"
  SECRET_KEY = "secret"
  </code></pre>
  
  <p>I should be able to request /auth/query and get the contents of the database table, but instead I get the error -
  "OperationalError: (sqlite3.OperationalError) no such table: researchers [SQL: u'INSERT INTO researchers (prefix, suffix, phone, phone_ext, orcid) VALUES (?, ?, ?, ?, ?)'] [parameters: ('Dr.', 'Mr', '08611111', '+353', '0001')] (Background on this error at: <a href="http://sqlalche.me/e/e3q8" rel="noreferrer">http://sqlalche.me/e/e3q8</a>)"</p>
  
  <p><a href="https://i.stack.imgur.com/Nytbm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Nytbm.png" alt="enter image description here"></a></p>