命令错误:使用Flask-Migrate迁移时,找不到由';.';标识的修订版本
问题描述
我今天开始使用Flask-Migrate,并将其安装在一个测试项目上。 但是,我收到以下错误:
alembi.util.exc.CommandError:找不到由标识的修订 ‘e39d16e62810’
复制步骤:
- 运行"python create_db.py"
- 运行"flask db init"
- 将"name"列添加到entry-model
- 运行"flask db迁移"
编辑:
在删除migrations
目录并重复该过程后,我在运行"flask db Migration"后得到相同的错误。
我还尝试将manage.py
文件与flask-script-->一起使用
错误:
(venv_mentz) H:Flask-API-Test>python manage.py db migrate
INFO [alembic.runtime.migration] Context impl SQLiteImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
Traceback (most recent call last):
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptase.py", line 143, in _catch_revision_errors
yield
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptase.py", line 206, in get_revisions
return self.revision_map.get_revisions(id_)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptevision.py", line 299, in get_revisions
return sum([self.get_revisions(id_elem) for id_elem in id_], ())
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptevision.py", line 299, in <listcomp>
return sum([self.get_revisions(id_elem) for id_elem in id_], ())
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptevision.py", line 304, in get_revisions
for rev_id in resolved_id)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptevision.py", line 304, in <genexpr>
for rev_id in resolved_id)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptevision.py", line 362, in _revision_for_ident
resolved_id)
alembic.script.revision.ResolutionError: No such revision or branch 'e39d16e62810'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "manage.py", line 14, in <module>
manager.run()
File "H:Flask-API-Testvenv_mentzlibsite-packagesflask_script\__init__.py", line 417, in run
result = self.handle(argv[0], argv[1:])
File "H:Flask-API-Testvenv_mentzlibsite-packagesflask_script\__init__.py", line 386, in handle
res = handle(*args, **config)
File "H:Flask-API-Testvenv_mentzlibsite-packagesflask_scriptcommands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "H:Flask-API-Testvenv_mentzlibsite-packagesflask_migrate\__init__.py", line 197, in migrate
version_path=version_path, rev_id=rev_id)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembiccommand.py", line 176, in revision
script_directory.run_env()
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptase.py", line 425, in run_env
util.load_python_file(self.dir, 'env.py')
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicutilpyfiles.py", line 81, in load_python_file
module = load_module_py(module_id, path)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicutilcompat.py", line 83, in load_module_py
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
File "migrationsenv.py", line 87, in <module>
run_migrations_online()
File "migrationsenv.py", line 80, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicuntimeenvironment.py", line 836, in run_migrations
self.get_context().run_migrations(**kw)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicuntimemigration.py", line 321, in run_migrations
for step in self._migrations_fn(heads, self):
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembiccommand.py", line 156, in retrieve_migrations
revision_context.run_autogenerate(rev, context)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicautogenerateapi.py", line 415, in run_autogenerate
self._run_environment(rev, migration_context, True)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicautogenerateapi.py", line 425, in _run_environment
if set(self.script_directory.get_revisions(rev)) !=
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptase.py", line 206, in get_revisions
return self.revision_map.get_revisions(id_)
File "c:usersmarschallappdatalocalprogramspythonpython36-32Libcontextlib.py", line 100, in __exit__
self.gen.throw(type, value, traceback)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptase.py", line 174, in _catch_revision_errors
compat.raise_from_cause(util.CommandError(resolution))
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicutilcompat.py", line 194, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=exc_value)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicutilcompat.py", line 187, in reraise
raise value.with_traceback(tb)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptase.py", line 143, in _catch_revision_errors
yield
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptase.py", line 206, in get_revisions
return self.revision_map.get_revisions(id_)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptevision.py", line 299, in get_revisions
return sum([self.get_revisions(id_elem) for id_elem in id_], ())
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptevision.py", line 299, in <listcomp>
return sum([self.get_revisions(id_elem) for id_elem in id_], ())
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptevision.py", line 304, in get_revisions
for rev_id in resolved_id)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptevision.py", line 304, in <genexpr>
for rev_id in resolved_id)
File "H:Flask-API-Testvenv_mentzlibsite-packagesalembicscriptevision.py", line 362, in _revision_for_ident
resolved_id)
alembic.util.exc.CommandError: Can't locate revision identified by 'e39d16e62810'
我的文件结构如下:
app
-- views
----- __init__.py
----- main.py
-- __init__.py
-- config.py
-- models.py
instance
-- __init__.py
-- config.py
create_db.py
dev.db
run.py
我的应用工厂:
from flask import Flask
from instance.config import app_config
from flask_migrate import Migrate
def create_app(config_name):
""" Creates a runnable app.
This app will be using the config with name "config_name".
"""
app = Flask(__name__)
# Loading the the config from instance folder with name "config_name"
app.config.from_object(app_config[config_name])
# Loading generic config from 'config.py'
app.config.from_pyfile('config.py')
# Registering this app at db
from app.models import db
db.init_app(app)
migrate = Migrate(app, db)
from app import models, views
return app
我的run.py:
""" This script runs a the app with the given configuration. """
from app import create_app
# Configuration used to run the app
config_name = 'dev'
# Creating the app by using the required configuration
app = create_app(config_name)
if __name__ == '__main__':
app.run()
和我的create_db.py文件使用mods.py:
创建数据库""" This script creates the database defined in app.models. """
from app import create_app
app = create_app('dev')
from app.models import db
# Telling SQLAlchemy what app should be used as the database model
with app.app_context():
db.create_all()
这是我的模型.py:
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Entry(db.Model):
__tablename__ = 'Entries'
layer_id = db.Column(db.Integer, primary_key=True)
def __repr__(self):
return "ID: {}; text: {}".format(self.layer_id, self.text)
编辑:manage.py:
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import create_app
from app import models
from app.models import db
app = create_app('dev')
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
解决方案
您删除了迁移目录,但是版本已经保存在数据库中,所以您必须删除数据库中的版本信息。 运行
delete from alembic_version;
在MySQL shell中。
根据@mirekphd的建议,如果这是开发环境或测试的单个应用,只需删除它,否则备份表中的数据。
相关文章