title: When scattering Flask Models, RuntimeError: 'application not registered on db' was raised
body: <p>I am re-factoring my Flask application by scattering the models, blueprints but I am having a runtime error. </p>
  
  <pre><code>def create_app():
      app = flask.Flask("app")
      app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://'
      app.register_blueprint(api)
      db.init_app(app)
      db.create_all()
      return app
  </code></pre>
  
  <p>I have the following problem(the sample project are hosted here: <a href="https://github.com/chfw/sample" rel="noreferrer">https://github.com/chfw/sample</a>):</p>
  
  <pre><code>Traceback (most recent call last):
    File "application.py", line 17, in &lt;module&gt;
      app = create_app()
    File "application.py", line 12, in create_app
      db.create_all()
    File "\AppData\Roaming\Python\Python27\site-packages\flask_sqlalchemy\__init__.py", line 856, in create_all
      self._execute_for_all_tables(app, bind, 'create_all')
    File "\AppData\Roaming\Python\Python27\site-packages\flask_sqlalchemy\__init__.py", line 836, in _execute_for_all_tables
      app = self.get_app(app)
    File "\AppData\Roaming\Python\Python27\site-packages\flask_sqlalchemy\__init__.py", line 809, in get_app
      raise RuntimeError('application not registered on db 
             'RuntimeError: application not registered on db 
              instance and no application bound to current context
  </code></pre>
  
  <p>I did a research on this topic. The re-factoring is suggested here:</p>
  
  <p><a href="https://stackoverflow.com/questions/9692962/flask-sqlalchemy-import-context-issue">Flask-SQLAlchemy import/context issue</a></p>
  
  <p>The same problem was raised here:</p>
  
  <p><a href="http://flask.pocoo.org/mailinglist/archive/2010/8/30/sqlalchemy-init-app-problem/#b1c3beb68573efef4d6e571ebc68fa0b" rel="noreferrer">http://flask.pocoo.org/mailinglist/archive/2010/8/30/sqlalchemy-init-app-problem/#b1c3beb68573efef4d6e571ebc68fa0b</a></p>
  
  <p>And the above thread(2010) suggested a hack like this:</p>
  
  <pre><code>    app.register_blueprint(api)
      db.app=app #&lt;------------&lt;&lt;
      db.init_app(app)
  </code></pre>
  
  <p>Did anyone know how to do this properly? How did you solve it?</p>
  
  <p>Thanks</p>