title: Alembic autogenerates empty Flask-SQLAlchemy migrations
body: <p>I'm using Alembic to handle migrations for Flask. <code>alembic revision --autogenerate</code> should, in theory, autogenerate a migration based on changes in my database. However, Alembic is simply generating a blank migration with the above command.</p>
  
  <p>There's a <a href="https://stackoverflow.com/questions/15660676/alembic-autogenerate-producing-empty-migration">question very similar to this one</a>, where the issue was that the proper models weren't being imported. However, I have imported the models from my Flask app, as shown in <code>env.py</code>:</p>
  
  <pre><code>...
  # import settings from Flask
  alembic_config = config.get_section(config.config_ini_section)
  from start import app
  from models import User, Item, Recipient # models are imported here from models.py
  alembic_config['sqlalchemy.url'] = app.config['SQLALCHEMY_DATABASE_URI']
  
  engine = engine_from_config(
              alembic_config, # config.get_section(config.config_ini_section)
              prefix='sqlalchemy.',
              poolclass=pool.NullPool)
  ...
  </code></pre>
  
  <p>As well as imported db metadata in <code>env.py</code> ('start' is the name of my Flask app's main file):</p>
  
  <pre><code>...
  from start import db
  target_metadata = db.metadata
  ...
  </code></pre>
  
  <p>Running <code>alembic revision --autogenerate -m "initial_rev"</code> then generates an empty migration, although my Flask app would beg to differ:</p>
  
  <pre><code>"""initial_rev
  
  Revision ID: 45296fd29540
  Revises: None
  Create Date: 2013-06-19 17:32:38.392268
  
  """
  
  # revision identifiers, used by Alembic.
  revision = '45296fd29540'
  down_revision = None
  
  from alembic import op
  import sqlalchemy as sa
  
  
  def upgrade():
      ### commands auto generated by Alembic - please adjust! ###
      pass
      ### end Alembic commands ###
  
  
  def downgrade():
      ### commands auto generated by Alembic - please adjust! ###
      pass
      ### end Alembic commands ###
  </code></pre>
  
  <h1>Edit</h1>
  
  <p><a href="https://gist.github.com/autibyte/5826111" rel="noreferrer">Here is a gist</a> that shows the file structure for my app, as well as some additional code. It seems the issue is that Alembic does not like having the db imported from <code>database.py</code> without being initialized first in <code>__init__.py</code>. However, this is not possible <em>when blueprints are used</em> (because of cyclical imports), explained in this SO answer: <a href="https://stackoverflow.com/a/9695045/353878">https://stackoverflow.com/a/9695045/353878</a>.</p>
  
  <p>So the question is, how can Alembic be used when Flask blueprints are used as well?</p>
  
  <h1>Edit #2</h1>
  
  <p>I even tried printing <code>db.metadata.sorted_tables</code>, to make sure the database metadata was being imported correctly. Sure enough, the whole database schema was piped to the terminal. So why is Alembic generating blank upgrade/downgrade functions?</p>
  
  <h1>Edit #3</h1>
  
  <p>I've concluded that the issue has something to do with the differences of <code>db.init_app(app)</code> and <code>db = SQLAlchemy(app)</code>, but I'm not quite sure what is causing the issue. To test this theory, I replaced <code>from database import db</code> in <code>env.py</code> to be <code>db = SQLAlchemy(app)</code>. Probably a bad idea, but I wanted to see what would happen for debugging purposes.</p>
  
  <p>Alembic autogenerated and filled the upgrade() and downgrade() methods- except they were reversed! <code>upgrade()</code> dropped all three of my tables, while <code>downgrade()</code> created them with all the proper columns and metadata. I have no idea why this is, but I hope it is helpful to people trying to figure out this problem.</p>