title: Flask, SQLAlchemy, Data Tables - ImportError: cannot import name 'db' in Models.py
body: <p>I'm trying to use Data Tables with Flask and SQLAchemy and I'm facing the</p>
  
  <p>ImportError: cannot import name 'db'</p>
  
  <p>in module Models.py</p>
  
  <p>My project tree:</p>
  
  <pre><code>app
      /common
          __init__.py
          models.py
          toutes.py
      /mod_tables
          ---
      __init__
      config.py
  </code></pre>
  
  <p>__init__.py  </p>
  
  <pre><code>    from flask import Flask, redirect, session
      from app.mod_tables.models import TableBuilder
      from app.config import Config
      from flask_sqlalchemy import SQLAlchemy
      #from flask_migrate import Migrate
  
      app = Flask(__name__)
  
      table_builder = TableBuilder()
  
      app.config.from_object(Config)
      db = SQLAlchemy(app)
      db.init_app(app)
      #migrate = Migrate(app, db)
  
      from app.common.routes import main
      from app.common import models
      from app.mod_tables.controllers import tables
  
      # Register the different blueprints
      app.register_blueprint(main)
      app.register_blueprint(tables)
  </code></pre>
  
  <p>config.py</p>
  
  <pre><code>import os
  
  basedir = os.path.abspath(os.path.dirname(__file__))
  
  class Config(object):
  
      SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
          'sqlite:///' + os.path.join(basedir, 'spbData-V3560-FRANCO.db')
      SQLALCHEMY_TRACK_MODIFICATIONS = False
  </code></pre>
  
  <p>\common.models.py</p>
  
  <pre><code>from app import db
  
  class TipoPoste(db.Model):
      tp_tipo = db.Column(db.String(35), primary_key=True)
      tp_descricao = db.Column(db.String(255))
      def __repr__(self):
          return '&lt;Tipo Poste {} - {}&gt;'.format(self.tp_tipo, 
              self.tp_descricao)
  </code></pre>
  
  <p>Thos code gives me the following error:</p>
  
  <pre><code>flask.cli.NoAppException flask.cli.NoAppException: While importing
  "app", an ImportError was raised:
  
  Traceback (most recent call last):   File
  "c:\users\rfran.v3560-franco\appdata\local\programs\python\python36-32\lib\site-packages\flask\cli.py",
  line 235, in locate_app
       __import__(module_name)   File "C:\Users\rfran.V3560-FRANCO\OneDrive\ArquivosLocais\gepoc\app\__init__.py",
  line 2, in &lt;module&gt;
      from app.mod_tables.models import TableBuilder   File "C:\Users\rfran.V3560-FRANCO\OneDrive\ArquivosLocais\gepoc\app\mod_tables\models.py",
  line 3, in &lt;module&gt;
      from app.common.models import TipoPoste   File "C:\Users\rfran.V3560-FRANCO\OneDrive\ArquivosLocais\gepoc\app\common\models.py",
  line 1, in &lt;module&gt;
      from app import db ImportError: cannot import name 'db'
  </code></pre>
  
  <p>Any hint? Thanks in advance.</p>