title: Flask and SQLAlchemy, application not registered on instance
body: <p>I'm currently trying to piece together a small Flask application. This is my structure.</p>
  
  <pre><code>run.py
  application
    __init__.py
    database.py
    models.py
    views.py
  </code></pre>
  
  <p><code>database.py</code> contains just the SQLAlchemy object:</p>
  
  <pre><code>db = SQLAlchemy()
  </code></pre>
  
  <p>I then import this into my <code>models.py</code> to create my models. Lastly, inside <code>__init__.py</code> I import <code>db</code> from <code>database.py</code> and do:</p>
  
  <pre><code>from .database import db
  from flask import Flask
  app = Flask(__name__)
  app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///application.db'
  db.init_app(app)
  db.create_all()
  </code></pre>
  
  <p>However, I cannot create the tables from the models it appears. If I remove <code>db.create_all()</code>. The application will run with no problems, but obviously the database is not created. When <code>db.create_all()</code> is present I am given 'RuntimeError: application not registered on db instance and no application bound to current context'.</p>
  
  <p>I'm honestly confused, as before I was having issues just starting the application without creating the database, but moving <code>db</code> to it's own file seems to somehow have corrected that issue. Now, the only issue remains is actually creating the database.</p>
  
  <p>Can anyone tell me what might be the problem? I'm genuinely stumped.</p>