数据库迁移表时报错:alembic.util.exc.CommandError: Target database is not up to date.

报错代码

INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
Traceback (most recent call last):
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/bin/flask", line 11, in 
    sys.exit(main())
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/flask/cli.py", line 894, in main
    cli.main(args=args, prog_name=name)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/flask/cli.py", line 557, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/click/core.py", line 717, in main
    rv = self.invoke(ctx)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/click/core.py", line 1137, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/click/core.py", line 956, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/click/decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/flask/cli.py", line 412, in decorator
    return __ctx.invoke(f, *args, **kwargs)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/click/core.py", line 555, in invoke
    return callback(*args, **kwargs)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/flask_migrate/cli.py", line 90, in migrate
    rev_id, x_arg)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/flask_migrate/__init__.py", line 197, in migrate
    version_path=version_path, rev_id=rev_id)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/alembic/command.py", line 176, in revision
    script_directory.run_env()
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/alembic/script/base.py", line 427, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/alembic/util/pyfiles.py", line 81, in load_python_file
    module = load_module_py(module_id, path)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/alembic/util/compat.py", line 145, in load_module_py
    spec.loader.exec_module(module)
  File "", line 678, in exec_module
  File "", line 219, in _call_with_frames_removed
  File "migrations/env.py", line 87, in 
    run_migrations_online()
  File "migrations/env.py", line 80, in run_migrations_online
    context.run_migrations()
  File "", line 8, in run_migrations
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/alembic/runtime/environment.py", line 836, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/alembic/runtime/migration.py", line 321, in run_migrations
    for step in self._migrations_fn(heads, self):
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/alembic/command.py", line 156, in retrieve_migrations
    revision_context.run_autogenerate(rev, context)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/alembic/autogenerate/api.py", line 415, in run_autogenerate
    self._run_environment(rev, migration_context, True)
  File "/Users/FiveMeter/Desktop/kaoputou-project/venv/Hulk-venv/lib/python3.6/site-packages/alembic/autogenerate/api.py", line 427, in _run_environment
    raise util.CommandError("Target database is not up to date.")
alembic.util.exc.CommandError: Target database is not up to date.

解决办法

1、先到项目目录下的migrations/version下,找到最新的migrate版本号
数据库迁移表时报错:alembic.util.exc.CommandError: Target database is not up to date._第1张图片
2、然后更新数据库中的alembic_version表的version_num字段的值为最新migrate版本号。
update alembic_version set version_num = '2620f11b3f67';
3、之后就可以正常的migrate和upgrade了

你可能感兴趣的:(数据库迁移表时报错:alembic.util.exc.CommandError: Target database is not up to date.)