title: Flask/SQL Alchemy: No application found. Either work inside a view function or push an application context
body: <p>I'm trying to replicate this <a href="https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xxiii-application-programming-interfaces-apis" rel="nofollow noreferrer">https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xxiii-application-programming-interfaces-apis</a> focus only in the API and security (tokens) part.</p>
  
  <p>I'm having problems when I'm executing the example to manage the <a href="https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database" rel="nofollow noreferrer">database</a></p>
  
  <pre><code>&gt;&gt;&gt; u = User(username='susan', email='susan@example.com')
  &gt;&gt;&gt; db.session.add(u)
  &gt;&gt;&gt; db.session.commit()
  </code></pre>
  
  <p>I get this error:</p>
  
  <pre><code>RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.
  </code></pre>
  
  <p><strong>The main idea here is to validate if the db is working properly</strong></p>
  
  <p>This is the code in my main app/<strong>init</strong>.py file:</p>
  
  <pre><code>from flask import Flask, request, current_app
  from flask_sqlalchemy import SQLAlchemy
  from flask_migrate import Migrate
  from config import Config
  
  db = SQLAlchemy()
  migrate = Migrate()
  
  def create_app(config_class=Config):
      app = Flask(__name__)
      app.config.from_object(config_class)
  
      db.init_app(app)
      migrate.init_app(app, db)
  
      from app.main import bp as main_bp
      app.register_blueprint(main_bp)
  
      from app.api import bp as api_bp
      app.register_blueprint(api_bp, url_prefix='/api')
  
      return app
  
  from app import models
  </code></pre>
  
  <p>This is my app/models.py file:</p>
  
  <pre><code>from flask import current_app, url_for
  from werkzeug.security import generate_password_hash, check_password_hash
  from app import db#, login
  
  class User(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      username = db.Column(db.String(64), index=True, unique=True)
      email = db.Column(db.String(120), index=True, unique=True)
      password_hash = db.Column(db.String(128))
      token = db.Column(db.String(32), index=True, unique=True)
      token_expiration = db.Column(db.DateTime)
  
      def __repr__(self):
          return '&lt;User {}&gt;'.format(self.username)
  
      def set_password(self, password):
          self.password_hash = generate_password_hash(password)
  
      def check_password(self, password):
          return check_password_hash(self.password_hash, password)
  
  
      def from_dict(self, data, new_user=False):
          for field in ['username', 'email']:
              if field in data:
                  setattr(self, field, data[field])
          if new_user and 'password' in data:
              self.set_password(data['password'])
  
  
      @staticmethod
      def check_token(token):
          user = User.query.filter_by(token=token).first()
          if user is None or user.token_expiration &lt; datetime.utcnow():
              return None
          return user
  </code></pre>