I'm using Flask Migrate with SQLite3. I have the following upgrade script:
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 ###
I initialise the database as follows:
convention = {
"ix": "ix_%(column_0_label)s",
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s"
}
metadata = MetaData(naming_convention=convention)
db = SQLAlchemy(app, metadata = metadata)
migrate = Migrate(app, db, render_as_batch = True)
When running the migration I get the following error:
Traceback (most recent call last):
File "C:\Users\Henry\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 193, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Users\Henry\AppData\Local\Programs\Python\Python38-32\lib\runpy.py", line 86, in _run_code
exec(code, run_globals)
File "C:\Users\Henry\Documents\Python\quiz\venv\Scripts\flask.exe\__main__.py", line 9, in <module>
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\flask\cli.py", line 988, in main
cli.main()
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\flask\cli.py", line 579, in main
return super().main(*args, **kwargs)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\core.py", line 1055, in main
rv = self.invoke(ctx)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\core.py", line 1657, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\core.py", line 1404, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\decorators.py", line 26, in new_func
return f(get_current_context(), *args, **kwargs)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\flask\cli.py", line 427, in decorator
return __ctx.invoke(f, *args, **kwargs)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\click\core.py", line 760, in invoke
return __callback(*args, **kwargs)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\flask_migrate\cli.py", line 149, in upgrade
_upgrade(directory, revision, sql, tag, x_arg)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\flask_migrate\__init__.py", line 98, in wrapped
f(*args, **kwargs)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\flask_migrate\__init__.py", line 185, in upgrade
command.upgrade(config, revision, sql=sql, tag=tag)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\command.py", line 322, in upgrade
script.run_env()
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\script\base.py", line 569, in run_env
util.load_python_file(self.dir, "env.py")
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\util\pyfiles.py", line 94, in load_python_file
module = load_module_py(module_id, path)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\util\pyfiles.py", line 110, in load_module_py
spec.loader.exec_module(module) # type: ignore
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "migrations\env.py", line 91, in <module>
run_migrations_online()
File "migrations\env.py", line 85, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\runtime\environment.py", line 853, in run_migrations
self.get_context().run_migrations(**kw)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\runtime\migration.py", line 623, in run_migrations
step.migration_fn(**kw)
File "C:\Users\Henry\Documents\Python\quiz\migrations\versions\e0b1170b3c02_.py", line 23, in upgrade
batch_op.drop_constraint(None, type_='foreignkey')
File "<string>", line 3, in drop_constraint
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\operations\ops.py", line 244, in batch_drop_constraint
return operations.invoke(op)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\operations\base.py", line 394, in invoke
return fn(self, operation)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\operations\toimpl.py", line 185, in drop_constraint
operations.schema_obj.generic_constraint(
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\alembic\operations\schemaobj.py", line 182, in generic_constraint
t.append_constraint(const)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 922, in append_constraint
constraint._set_parent_with_dispatch(self)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\sql\base.py", line 1047, in _set_parent_with_dispatch
self.dispatch.after_parent_attach(self, parent)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\event\attr.py", line 256, in __call__
fn(*args, **kw)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\sql\naming.py", line 208, in _constraint_name
newname = _constraint_name_for_table(const, table)
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\sql\naming.py", line 166, in _constraint_name_for_table
convention
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\sql\naming.py", line 101, in __getitem__
return getattr(self, "_key_%s" % key)()
File "C:\Users\Henry\Documents\Python\quiz\venv\lib\site-packages\sqlalchemy\sql\naming.py", line 82, in _key_referred_table_name
fk = self.const.elements[0]
IndexError: list index out of range
I was trying to change session_id = db.Column(db.Integer, db.ForeignKey("quiz_session.quiz_id"))
in my QuizPlayer
model to session_id = db.Column(db.Integer, db.ForeignKey("quiz_session.session_id"))
. The migration gave an error because I wasn't using batch mode. I added render_as_batch = True
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.
I commented out
batch_op.drop_constraint(None, type_='foreignkey')
and that seemed to fix it.