CommandError:使用Flask-Migrate迁移时无法找到由'...'标识的修订版

时间:2017-12-05 14:32:58

标签: python flask flask-sqlalchemy flask-migrate

我今天开始使用Flask-Migrate并将其安装在测试项目中。 但是我得到了以下错误:

  

alembic.util.exc.CommandError:找不到标识的修订版   'e39d16e62810'

重现的步骤:

  1. 运行“python create_db.py”
  2. 运行“flask db init”
  3. 将列“name”添加到Entry-model
  4. 运行“flask db migrate”
  5. 修改 删除M____ACRO_Storage____01234目录并重复该过程后,我在运行“flask db migrate”后收到相同的错误。 我还尝试使用带烧瓶脚本的migrations文件 - >同样的问题

    错误:

    manage.py

    我的文件结构如下所示:

    (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-Test\venv_mentz\lib\site-packages\alembic\script\base.py", line 143, in _catch_revision_errors
        yield
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\base.py", line 206, in get_revisions
        return self.revision_map.get_revisions(id_)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\revision.py", line 299, in get_revisions
        return sum([self.get_revisions(id_elem) for id_elem in id_], ())
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\revision.py", line 299, in <listcomp>
        return sum([self.get_revisions(id_elem) for id_elem in id_], ())
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\revision.py", line 304, in get_revisions
        for rev_id in resolved_id)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\revision.py", line 304, in <genexpr>
        for rev_id in resolved_id)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\revision.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-Test\venv_mentz\lib\site-packages\flask_script\__init__.py", line 417, in run
        result = self.handle(argv[0], argv[1:])
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\flask_script\__init__.py", line 386, in handle
        res = handle(*args, **config)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\flask_script\commands.py", line 216, in __call__
        return self.run(*args, **kwargs)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\flask_migrate\__init__.py", line 197, in migrate
        version_path=version_path, rev_id=rev_id)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\command.py", line 176, in revision
        script_directory.run_env()
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\base.py", line 425, in run_env
        util.load_python_file(self.dir, 'env.py')
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\util\pyfiles.py", line 81, in load_python_file
        module = load_module_py(module_id, path)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\util\compat.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 "migrations\env.py", line 87, in <module>
        run_migrations_online()
      File "migrations\env.py", line 80, in run_migrations_online
        context.run_migrations()
      File "<string>", line 8, in run_migrations
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\runtime\environment.py", line 836, in run_migrations
        self.get_context().run_migrations(**kw)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\runtime\migration.py", line 321, in run_migrations
        for step in self._migrations_fn(heads, self):
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\command.py", line 156, in retrieve_migrations
        revision_context.run_autogenerate(rev, context)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\autogenerate\api.py", line 415, in run_autogenerate
        self._run_environment(rev, migration_context, True)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\autogenerate\api.py", line 425, in _run_environment
        if set(self.script_directory.get_revisions(rev)) != \
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\base.py", line 206, in get_revisions
        return self.revision_map.get_revisions(id_)
      File "c:\users\marschall\appdata\local\programs\python\python36-32\Lib\contextlib.py", line 100, in __exit__
        self.gen.throw(type, value, traceback)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\base.py", line 174, in _catch_revision_errors
        compat.raise_from_cause(util.CommandError(resolution))
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\util\compat.py", line 194, in raise_from_cause
        reraise(type(exception), exception, tb=exc_tb, cause=exc_value)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\util\compat.py", line 187, in reraise
        raise value.with_traceback(tb)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\base.py", line 143, in _catch_revision_errors
        yield
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\base.py", line 206, in get_revisions
        return self.revision_map.get_revisions(id_)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\revision.py", line 299, in get_revisions
        return sum([self.get_revisions(id_elem) for id_elem in id_], ())
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\revision.py", line 299, in <listcomp>
        return sum([self.get_revisions(id_elem) for id_elem in id_], ())
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\revision.py", line 304, in get_revisions
        for rev_id in resolved_id)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\revision.py", line 304, in <genexpr>
        for rev_id in resolved_id)
      File "H:\Flask-API-Test\venv_mentz\lib\site-packages\alembic\script\revision.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
    

    我的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
    

    我使用models.py创建数据库的create_db.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()
    

    这是我的models.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()
    

    编辑:manage.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)
    

10 个答案:

答案 0 :(得分:2)

删除迁移目录但版本已保存在数据库中,因此您必须删除数据库中的版本信息, 跑 从alembic_version删除;

在mysql shell中。

答案 1 :(得分:2)

在大多数情况下,如果您处于开发阶段而没有太多数据要散发,则可以删除数据库并创建一个新的数据库,或者,您可以删除迁移并开始整个迁移再次循环,对于sqlite,这完全是关于删除应用程序中的sqlite文件,然后再次开始该循环。我希望尽快找到最佳解决方案。

答案 2 :(得分:2)

我在烧瓶上试试这个,它解决了!

<块引用>

python app.py db 修订版 --rev-id e39d16e62810
python app.py db migrate
python app.py db升级

答案 3 :(得分:1)

我找到了解决方案。我的错误....数据库的路径是错误的。无论如何,谢谢你。

答案 4 :(得分:1)

从数据库中删除历史记录条目将解决此问题。

错误:alembic.util.exc.CommandError:找不到由'e39d16e62810'标识的修订版本

select * from alembic_version where verion_num='e39d16e62810'

然后删除该记录:

delete from alembic_version where verion_num='e39d16e62810'

答案 5 :(得分:0)

就我而言,由于我处于初始阶段,因此可以直接删除数据库文件,即在我的项目文件夹中有一个.sqlite文件,因此我通过右键单击将其删除。 但是请记住,我处于初始阶段,因此如果您是 不在开始阶段。

答案 6 :(得分:0)

就我而言,导致错误的原因是sqlite db的相对路径。

通过指定绝对路径修复了该问题

app.config['SQLALCHEMY_DATABASE_URI'] = f"sqlite:///{os.path.join(os.path.dirname(__file__), os.path.pardir, 'db.sqlite')}"

答案 7 :(得分:0)

就我而言,我不小心删除了最新的迁移文件,但alembic版本(alembic_version)表是指已删除的版本。

因此,除了删除整个数据库,您还可以更改alembic_version表中的version_num字段。

以下步骤对我有用:

  
      
  • 使用db history
  • 查找头部   
  • 将version_num字段更新为主要版本。
  •   
  • 使用db migrate
  • 运行迁移   
  • 升级数据库db upgrade
  •   

答案 8 :(得分:0)

我有相同的错误消息,但就我而言,这是因为我在连接字符串中使用了错误的数据库名称。检查您的连接字符串

错误消息:

错误[root]错误:找不到由'a80ab7ca5e1a'标识的修订版本

原因: 我的连接字符串是:

mysql + pymysql:// kaunda:kaunda @ localhost:3306 / smis

代替

mysql + pymysql:// kaunda:kaunda @ localhost:3306 / WorkFolder

由“ a80ab7ca5e1a”标识的修订版属于数据库“ smis”而不是“ WorkFolder”

答案 9 :(得分:0)

另一个引起问题的原因-在同一个虚拟环境中创建两个flask项目。

我试图制作假双胞胎项目进行性能测试。

  • 复制了原始项目的关键部分
  • 创建了新数据库
  • 更改配置文件以指向新数据库
  • flask db init
  • flask db migrate
  • 在第一次迁移时遇到此错误。

然后我只做一件事-为这个孪生项目创建了新的python环境-错误消失了。

PS。也许可以在同一环境中创建两个flask项目,但是我找不到解决方案。