Вопреки здравому смыслу, я решил переименовать основной столбец в моих базах данных с id
на key
, поскольку я не хотел конфликтовать с функцией id
Python.
Мне удалосьиспользуйте alembic для переименования столбца с помощью пакетных миграций , так как я использую бэкэнд SQLite.
Теперь при добавлении новой таблицы в мою модель alembic
удается обнаружить новуютаблица, а затем происходит сбой со следующим:
sqlalchemy.exc.NoReferencedColumnError: Could not initialize target column for ForeignKey 'tests.id' on table 'runs': table 'tests' has no column named 'id'
У меня есть таблица с именем tests и таблица с именем run, которые связаны через внешний ключ:
class Base():
key = Column(Integer, primary_key=True)
BASE = declarative_base(cls=Base)
class Test(BASE):
runs = relationship("Run", back_populates="test",
cascade="all, delete, delete-orphan")
class Run(BASE):
test_id = Column(Integer, ForeignKey("tests.key"))
test = relationship("Test", back_populates="runs")
Похоже на alembic
пытается воссоздать все миграции при попытке автогенерации.Или хотя бы создание таблицы.
Я что-то не так делаю?Или это ошибка в alembic?
Ниже приводится полный вывод команды с отредактированными путями.
$ alembic -n testing revision --autogenerate -m "Add sessions table"
INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.autogenerate.compare] Detected added table 'sessions'
Traceback (most recent call last):
File "bin/alembic", line 10, in <module>
sys.exit(main())
File "lib64/python3.6/site-packages/alembic/config.py", line 540, in main
CommandLine(prog=prog).main(argv=argv)
File "lib64/python3.6/site-packages/alembic/config.py", line 534, in main
self.run_cmd(cfg, options)
File "lib64/python3.6/site-packages/alembic/config.py", line 514, in run_cmd
**dict((k, getattr(options, k, None)) for k in kwarg)
File "lib64/python3.6/site-packages/alembic/command.py", line 197, in revision
script_directory.run_env()
File "lib64/python3.6/site-packages/alembic/script/base.py", line 475, in run_env
util.load_python_file(self.dir, "env.py")
File "lib64/python3.6/site-packages/alembic/util/pyfiles.py", line 90, in load_python_file
module = load_module_py(module_id, path)
File "lib64/python3.6/site-packages/alembic/util/compat.py", line 177, in load_module_py
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "alembic/env.py", line 71, in <module>
run_migrations_online()
File "alembic/env.py", line 65, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "lib64/python3.6/site-packages/alembic/runtime/environment.py", line 839, in run_migrations
self.get_context().run_migrations(**kw)
File "lib64/python3.6/site-packages/alembic/runtime/migration.py", line 351, in run_migrations
for step in self._migrations_fn(heads, self):
File "lib64/python3.6/site-packages/alembic/command.py", line 173, in retrieve_migrations
revision_context.run_autogenerate(rev, context)
File "lib64/python3.6/site-packages/alembic/autogenerate/api.py", line 433, in run_autogenerate
self._run_environment(rev, migration_context, True)
File "lib64/python3.6/site-packages/alembic/autogenerate/api.py", line 473, in _run_environment
autogen_context, migration_script
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 25, in _populate_migration_script
_produce_net_changes(autogen_context, upgrade_ops)
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 51, in _produce_net_changes
autogen_context, upgrade_ops, schemas
File "lib64/python3.6/site-packages/alembic/util/langhelpers.py", line 303, in go
fn(*arg, **kw)
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 83, in _autogen_for_tables
autogen_context,
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 225, in _compare_tables
metadata_table,
File "lib64/python3.6/site-packages/alembic/util/langhelpers.py", line 303, in go
fn(*arg, **kw)
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 994, in _compare_foreign_keys
for fk in conn_fks
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 994, in <genexpr>
for fk in conn_fks
File "lib64/python3.6/site-packages/alembic/autogenerate/compare.py", line 400, in __init__
) = _fk_spec(const)
File "lib64/python3.6/site-packages/alembic/util/sqla_compat.py", line 77, in _fk_spec
target_schema = constraint.elements[0].column.table.schema
File "lib64/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 855, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "lib64/python3.6/site-packages/sqlalchemy/sql/schema.py", line 2039, in column
colname,
sqlalchemy.exc.NoReferencedColumnError: Could not initialize target column for ForeignKey 'tests.id' on table 'runs': table 'tests' has no column named 'id'