title: Flask Migrate - IndexError on migration
body: <p>I'm using Flask Migrate with SQLite3. I have the following upgrade script:</p>
  <pre><code>from alembic import op
  import sqlalchemy as sa
  
  
  # revision identifiers, used by Alembic.
  revision = 'e0b1170b3c02'
  down_revision = '9fac07025424'
  branch_labels = None
  depends_on = None
  
  
  def upgrade():
      # ### commands auto generated by Alembic - please adjust! ###
      with op.batch_alter_table('quiz_player', schema=None) as batch_op:
          batch_op.create_unique_constraint(batch_op.f('uq_quiz_player_player_id'), ['player_id'])
          batch_op.drop_constraint(None, type_='foreignkey')
          batch_op.create_foreign_key(batch_op.f('fk_quiz_player_session_id_quiz_session'), 'quiz_session', ['session_id'], ['session_id'])
  
      with op.batch_alter_table('quiz_session', schema=None) as batch_op:
          batch_op.create_unique_constraint(batch_op.f('uq_quiz_session_session_id'), ['session_id'])
          batch_op.create_unique_constraint(batch_op.f('uq_quiz_session_shortcode'), ['shortcode'])
  
      # ### end Alembic commands ###
  
  
  def downgrade():
      # ### commands auto generated by Alembic - please adjust! ###
      with op.batch_alter_table('quiz_session', schema=None) as batch_op:
          batch_op.drop_constraint(batch_op.f('uq_quiz_session_shortcode'), type_='unique')
          batch_op.drop_constraint(batch_op.f('uq_quiz_session_session_id'), type_='unique')
  
      with op.batch_alter_table('quiz_player', schema=None) as batch_op:
          batch_op.drop_constraint(batch_op.f('fk_quiz_player_session_id_quiz_session'), type_='foreignkey')
          batch_op.create_foreign_key(None, 'quiz_session', ['session_id'], ['quiz_id'])
          batch_op.drop_constraint(batch_op.f('uq_quiz_player_player_id'), type_='unique')
  
      # ### end Alembic commands ###
  </code></pre>
  <p>I initialise the database as follows:</p>
  <pre><code>convention = {
      &quot;ix&quot;: &quot;ix_%(column_0_label)s&quot;,
      &quot;uq&quot;: &quot;uq_%(table_name)s_%(column_0_name)s&quot;,
      &quot;ck&quot;: &quot;ck_%(table_name)s_%(constraint_name)s&quot;,
      &quot;fk&quot;: &quot;fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s&quot;,
      &quot;pk&quot;: &quot;pk_%(table_name)s&quot;
  }
  metadata = MetaData(naming_convention=convention)
  db = SQLAlchemy(app, metadata = metadata)
  migrate = Migrate(app, db, render_as_batch = True)
  </code></pre>
  <p>When running the migration I get the following error:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\Henry\AppData\Local\Programs\Python\Python38-32\lib\runpy.py&quot;, line 193, in _run_module_as_main
      return _run_code(code, main_globals, None,
    File &quot;C:\Users\Henry\AppData\Local\Programs\Python\Python38-32\lib\runpy.py&quot;, line 86, in _run_code
      exec(code, run_globals)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\Scripts\flask.exe\__main__.py&quot;, line 9, in &lt;module&gt;
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\flask\cli.py&quot;, line 988, in main
      cli.main()
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\flask\cli.py&quot;, line 579, in main
      return super().main(*args, **kwargs)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\core.py&quot;, line 1055, in main
      rv = self.invoke(ctx)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\core.py&quot;, line 1657, in invoke
      return _process_result(sub_ctx.command.invoke(sub_ctx))
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\core.py&quot;, line 1657, in invoke
      return _process_result(sub_ctx.command.invoke(sub_ctx))
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\core.py&quot;, line 1404, in invoke
      return ctx.invoke(self.callback, **ctx.params)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\core.py&quot;, line 760, in invoke
      return __callback(*args, **kwargs)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\decorators.py&quot;, line 26, in new_func
      return f(get_current_context(), *args, **kwargs)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\flask\cli.py&quot;, line 427, in decorator
      return __ctx.invoke(f, *args, **kwargs)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\core.py&quot;, line 760, in invoke
      return __callback(*args, **kwargs)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\flask_migrate\cli.py&quot;, line 149, in upgrade
      _upgrade(directory, revision, sql, tag, x_arg)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\flask_migrate\__init__.py&quot;, line 98, in wrapped
      f(*args, **kwargs)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\flask_migrate\__init__.py&quot;, line 185, in upgrade
      command.upgrade(config, revision, sql=sql, tag=tag)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\command.py&quot;, line 322, in upgrade
      script.run_env()
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\script\base.py&quot;, line 569, in run_env
      util.load_python_file(self.dir, &quot;env.py&quot;)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\util\pyfiles.py&quot;, line 94, in load_python_file
      module = load_module_py(module_id, path)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\util\pyfiles.py&quot;, line 110, in load_module_py
      spec.loader.exec_module(module)  # type: ignore
    File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 783, in exec_module
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 219, in _call_with_frames_removed
    File &quot;migrations\env.py&quot;, line 91, in &lt;module&gt;
      run_migrations_online()
    File &quot;migrations\env.py&quot;, line 85, in run_migrations_online
      context.run_migrations()
    File &quot;&lt;string&gt;&quot;, line 8, in run_migrations
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\runtime\environment.py&quot;, line 853, in run_migrations
      self.get_context().run_migrations(**kw)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\runtime\migration.py&quot;, line 623, in run_migrations
      step.migration_fn(**kw)
    File &quot;C:\Users\Henry\Documents\Python\quiz\migrations\versions\e0b1170b3c02_.py&quot;, line 23, in upgrade
      batch_op.drop_constraint(None, type_='foreignkey')
    File &quot;&lt;string&gt;&quot;, line 3, in drop_constraint
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\operations\ops.py&quot;, line 244, in batch_drop_constraint
      return operations.invoke(op)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\operations\base.py&quot;, line 394, in invoke
      return fn(self, operation)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\operations\toimpl.py&quot;, line 185, in drop_constraint
      operations.schema_obj.generic_constraint(
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\operations\schemaobj.py&quot;, line 182, in generic_constraint
      t.append_constraint(const)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\sql\schema.py&quot;, line 922, in append_constraint
      constraint._set_parent_with_dispatch(self)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\sql\base.py&quot;, line 1047, in _set_parent_with_dispatch
      self.dispatch.after_parent_attach(self, parent)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\event\attr.py&quot;, line 256, in __call__
      fn(*args, **kw)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\sql\naming.py&quot;, line 208, in _constraint_name
      newname = _constraint_name_for_table(const, table)
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\sql\naming.py&quot;, line 166, in _constraint_name_for_table
      convention
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\sql\naming.py&quot;, line 101, in __getitem__
      return getattr(self, &quot;_key_%s&quot; % key)()
    File &quot;C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\sql\naming.py&quot;, line 82, in _key_referred_table_name
      fk = self.const.elements[0]
  IndexError: list index out of range
  </code></pre>
  <p>I was trying to change <code>session_id = db.Column(db.Integer, db.ForeignKey(&quot;quiz_session.quiz_id&quot;))</code> in my <code>QuizPlayer</code> model to <code>session_id = db.Column(db.Integer, db.ForeignKey(&quot;quiz_session.session_id&quot;))</code>. The migration gave an error because I wasn't using batch mode. I added <code>render_as_batch = True</code> but then the migration failed because of unnamed constraints. I added a naming convention as suggested in other answers and now I have this error, presumably caused by the naming convention. This model was created before adding the naming convention and enabling batch mode which is why I assume it's not working.</p>