title: Prevent alembic auto-migration from being generated when there are no changes to the schema
body: <p>I am working on a flask application and using <a href="https://flask-migrate.readthedocs.io/en/latest/" rel="nofollow noreferrer">Flask-Migrate</a> with a snowflake database through the <a href="https://github.com/snowflakedb/snowflake-sqlalchemy" rel="nofollow noreferrer">snowflake-sqlalchemy</a> connector.</p>
  
  <p>I have the following models in my app/models.py:</p>
  
  <pre><code>class User(db.Model):
      username = db.Column(db.String(100), primary_key=True)
  
  
  class UserActions(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      user = db.Column(db.String(100), db.ForeignKey('user.username'))
  </code></pre>
  
  <p>As you can see, it's a simple 2 table example with one table having a foreign key constraint.</p>
  
  <p>The first time I run <code>flask db migrate</code>, I get the following autogenerated migration:</p>
  
  <pre><code>def upgrade():
      # ### commands auto generated by Alembic - please adjust! ###
      op.create_table('user',
      sa.Column('username', sa.String(length=100), nullable=False),
      sa.PrimaryKeyConstraint('username', name=op.f('pk_user'))
      )
      op.create_table('user_actions',
      sa.Column('id', sa.Integer(), nullable=False),
      sa.Column('user', sa.String(length=100), nullable=True),
      sa.ForeignKeyConstraint(['user'], ['user.username'], name=op.f('fk_user_actions_user_user')),
      sa.PrimaryKeyConstraint('id', name=op.f('pk_user_actions'))
      )
      # ### end Alembic commands ###
  </code></pre>
  
  <p>which looks great, and works as expected when executing <code>flask db upgrade</code>.</p>
  
  <p>But when I run <code>flask db migrate</code> a second time, even though there's no changes to schema, it produces another migration of </p>
  
  <pre><code>def upgrade():
      # ### commands auto generated by Alembic - please adjust! ###
      op.drop_constraint('fk_user_actions_user_user', 'user_actions', type_='foreignkey')
      op.create_foreign_key(op.f('fk_user_actions_user_user'), 'user_actions', 'user', ['user'], ['username'])
      # ### end Alembic commands ###
  
  def downgrade():
      # ### commands auto generated by Alembic - please adjust! ###
      op.drop_constraint(op.f('fk_user_actions_user_user'), 'user_actions', type_='foreignkey')
      op.create_foreign_key('fk_user_actions_user_user', 'user_actions', 'user', ['user'], ['username'], referent_schema='{my-schema}')
      # ### end Alembic commands ###
  </code></pre>
  
  <p>I have tried various configuration settings to the <code>metadata</code> bound to the flask <code>db</code> object, but I always get this redundant migration when using alembic's autogenerate feature. Even if I perform an upgrade using this redundant migration, I get another duplicate when running <code>flask db migrate</code> again. My connection string to the db does have schema in it, because snowflake complains otherwise:</p>
  
  <pre><code>SQLALCHEMY_DATABASE_URI='snowflake://&lt;my_login_name&gt;:&lt;password&gt;@&lt;account_name&gt;/&lt;database_name&gt;/&lt;schema_name&gt;?warehouse=&lt;warehouse_name&gt;?role=&lt;role_name&gt;'
  </code></pre>
  
  <p>What is going on here? Why is there a redundant migration being produced every single time even though there's no changes to the schema? I have read through <a href="https://github.com/snowflakedb/snowflake-sqlalchemy" rel="nofollow noreferrer">this link</a>, but that didn't help me. I don't specify schema in any of the models or the migration scripts, only in the connection url.</p>