之前用Flask-Migrate去升级Flask的数据库,已经在migrations下有version,且有个版本df80d262d138了
但是我觉得不想保留,就去删掉了:
(RunningFast) ➜ staging ll migrations total 24K -rw-r–r– 1 root root 770 Oct 10 18:02 alembic.ini -rw-r–r– 1 root root 2.9K Oct 10 18:02 env.py -rw-r–r– 1 root root 2.5K Oct 10 18:08 env.pyc -rw-r–r– 1 root root 38 Oct 10 18:02 README -rw-r–r– 1 root root 412 Oct 10 18:02 script.py.mako drwxr-xr-x 2 root root 4.0K Oct 13 15:11 versions (RunningFast) ➜ staging ll migrations/versions total 8.0K -rw-r–r– 1 root root 1011 Oct 13 15:11 df80d262d138_.py -rw-r–r– 1 root root 1.3K Oct 13 15:11 df80d262d138_.pyc (RunningFast) ➜ staging rm migrations/versions/* zsh: sure you want to delete all the files in /root/RunningFast/staging/migrations/versions [yn]? y (RunningFast) ➜ staging ll migrations/versions total 0 |
导致后续继续去用Flask-Migrate去升级数据库时,就报错了:
(RunningFast) ➜ staging python db_manager.py db migrate -m "add user location" <div–<—————————————————————————— DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:73]: app=<Flask ‘runningfast’>, api=<flask_restful.Api object at 0x7f3d678a84d0>, redis_store=<flask_redis.FlaskRedis object at 0x7f3d68f1d210>, db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>, server_mode=staging, server_type=develop, rq=<flask_rq2.app.RQ object at 0x7f3d678a8790> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:83]: UPDATE_USER_STATUS_INTERVAL_SECONDS=5.0, MAX_OFFLINE_INTERVAL_SECONDS=10.0 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in models [/root/RunningFast/staging/runningfast/models.py:13]: prefix=user-, generatedUuid4Str=0950829a-62c9-4ce4-bdaa-7704e5af5ced, newUuid=user-0950829a-62c9-4ce4-bdaa-7704e5af5ced <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/root/RunningFast/staging/runningfast/views.py:45]: API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open <div–<—————————————————————————— INFO [alembic.runtime.migration] Context impl MySQLImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. Traceback (most recent call last): File "db_manager.py", line 18, in <module> manager.run() File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run result = self.handle(sys.argv[0], sys.argv[1:]) File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle res = handle(*args, **config) File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__ return self.run(*args, **kwargs) File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask_migrate/__init__.py", line 177, in migrate version_path=version_path, rev_id=rev_id) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/command.py", line 117, in revision script_directory.run_env() File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/base.py", line 407, in run_env util.load_python_file(self.dir, ‘env.py’) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file module = load_module_py(module_id, path) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/util/compat.py", line 79, in load_module_py mod = imp.load_source(module_id, path, fp) 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 "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/runtime/environment.py", line 797, in run_migrations self.get_context().run_migrations(**kw) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/runtime/migration.py", line 303, in run_migrations for step in self._migrations_fn(heads, self): File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/command.py", line 97, in retrieve_migrations revision_context.run_autogenerate(rev, context) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/autogenerate/api.py", line 369, in run_autogenerate self._run_environment(rev, migration_context, True) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/autogenerate/api.py", line 379, in _run_environment if set(self.script_directory.get_revisions(rev)) != \ File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/base.py", line 192, in get_revisions return self.revision_map.get_revisions(id_) File "/usr/local/lib/python2.7/contextlib.py", line 35, in __exit__ self.gen.throw(type, value, traceback) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/base.py", line 160, in _catch_revision_errors compat.raise_from_cause(util.CommandError(resolution)) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/util/compat.py", line 132, in raise_from_cause reraise(type(exception), exception, tb=exc_tb) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/base.py", line 129, in _catch_revision_errors yield File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/base.py", line 192, in get_revisions return self.revision_map.get_revisions(id_) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/revision.py", line 298, in get_revisions return sum([self.get_revisions(id_elem) for id_elem in id_], ()) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/revision.py", line 303, in get_revisions for rev_id in resolved_id) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/revision.py", line 303, in <genexpr> for rev_id in resolved_id) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/revision.py", line 358, in _revision_for_ident resolved_id) alembic.util.exc.CommandError: Can’t locate revision identified by ‘df80d262d138’ |
然后去试试init,结果提示migrations已存在:
(RunningFast) ➜ staging python db_manager.py db init <div–<—————————————————————————— DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:73]: app=<Flask ‘runningfast’>, api=<flask_restful.Api object at 0x7f13cf94d4d0>, redis_store=<flask_redis.FlaskRedis object at 0x7f13d0fc1210>, db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>, server_mode=staging, server_type=develop, rq=<flask_rq2.app.RQ object at 0x7f13cf94d790> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:83]: UPDATE_USER_STATUS_INTERVAL_SECONDS=5.0, MAX_OFFLINE_INTERVAL_SECONDS=10.0 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in models [/root/RunningFast/staging/runningfast/models.py:13]: prefix=user-, generatedUuid4Str=81a4bdcd-ae8b-4033-8260-ff95af3f1cd6, newUuid=user-81a4bdcd-ae8b-4033-8260-ff95af3f1cd6 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/root/RunningFast/staging/runningfast/views.py:45]: API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open <div–<—————————————————————————— Traceback (most recent call last): File "db_manager.py", line 18, in <module> manager.run() File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run result = self.handle(sys.argv[0], sys.argv[1:]) File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle res = handle(*args, **config) File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__ return self.run(*args, **kwargs) File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask_migrate/__init__.py", line 101, in init command.init(config, directory, ‘flask’) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/command.py", line 29, in init raise util.CommandError("Directory %s already exists" % directory) alembic.util.exc.CommandError: Directory migrations already exists |
自己干脆去删除掉整个的migrations文件夹:
(RunningFast) ➜ staging rm -rf migrations |
重新init,是可以了:
(RunningFast) ➜ staging python db_manager.py db init <div–<—————————————————————————— DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:73]: app=<Flask ‘runningfast’>, api=<flask_restful.Api object at 0x7fdcf37cd4d0>, redis_store=<flask_redis.FlaskRedis object at 0x7fdcf4e41210>, db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>, server_mode=staging, server_type=develop, rq=<flask_rq2.app.RQ object at 0x7fdcf37cd790> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:83]: UPDATE_USER_STATUS_INTERVAL_SECONDS=5.0, MAX_OFFLINE_INTERVAL_SECONDS=10.0 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in models [/root/RunningFast/staging/runningfast/models.py:13]: prefix=user-, generatedUuid4Str=7c10d9d3-083a-4768-8364-d999df215ea8, newUuid=user-7c10d9d3-083a-4768-8364-d999df215ea8 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/root/RunningFast/staging/runningfast/views.py:45]: API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open <div–<—————————————————————————— Creating directory /root/RunningFast/staging/migrations … done Creating directory /root/RunningFast/staging/migrations/versions … done Generating /root/RunningFast/staging/migrations/README … done Generating /root/RunningFast/staging/migrations/env.py … done Generating /root/RunningFast/staging/migrations/env.pyc … done Generating /root/RunningFast/staging/migrations/alembic.ini … done Generating /root/RunningFast/staging/migrations/script.py.mako … done Please edit configuration/connection/logging settings in ‘/root/RunningFast/staging/migrations/alembic.ini’ before proceeding. |
但是再去升级,错误依旧:
(RunningFast) ➜ staging python db_manager.py db migrate -m "add user location" <div–<—————————————————————————— DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:73]: app=<Flask ‘runningfast’>, api=<flask_restful.Api object at 0x7fc569a964d0>, redis_store=<flask_redis.FlaskRedis object at 0x7fc56b10b210>, db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>, server_mode=staging, server_type=develop, rq=<flask_rq2.app.RQ object at 0x7fc569a96790> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:83]: UPDATE_USER_STATUS_INTERVAL_SECONDS=5.0, MAX_OFFLINE_INTERVAL_SECONDS=10.0 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in models [/root/RunningFast/staging/runningfast/models.py:13]: prefix=user-, generatedUuid4Str=061c8588-74f8-42e9-b2ca-db476c6cb51c, newUuid=user-061c8588-74f8-42e9-b2ca-db476c6cb51c <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/root/RunningFast/staging/runningfast/views.py:45]: API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open <div–<—————————————————————————— INFO [alembic.runtime.migration] Context impl MySQLImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. Traceback (most recent call last): File "db_manager.py", line 18, in <module> manager.run() File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run result = self.handle(sys.argv[0], sys.argv[1:]) File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle res = handle(*args, **config) File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__ return self.run(*args, **kwargs) File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask_migrate/__init__.py", line 177, in migrate version_path=version_path, rev_id=rev_id) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/command.py", line 117, in revision script_directory.run_env() File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/base.py", line 407, in run_env util.load_python_file(self.dir, ‘env.py’) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file module = load_module_py(module_id, path) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/util/compat.py", line 79, in load_module_py mod = imp.load_source(module_id, path, fp) 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 "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/runtime/environment.py", line 797, in run_migrations self.get_context().run_migrations(**kw) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/runtime/migration.py", line 303, in run_migrations for step in self._migrations_fn(heads, self): File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/command.py", line 97, in retrieve_migrations revision_context.run_autogenerate(rev, context) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/autogenerate/api.py", line 369, in run_autogenerate self._run_environment(rev, migration_context, True) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/autogenerate/api.py", line 379, in _run_environment if set(self.script_directory.get_revisions(rev)) != \ File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/base.py", line 192, in get_revisions return self.revision_map.get_revisions(id_) File "/usr/local/lib/python2.7/contextlib.py", line 35, in __exit__ self.gen.throw(type, value, traceback) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/base.py", line 160, in _catch_revision_errors compat.raise_from_cause(util.CommandError(resolution)) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/util/compat.py", line 132, in raise_from_cause reraise(type(exception), exception, tb=exc_tb) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/base.py", line 129, in _catch_revision_errors yield File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/base.py", line 192, in get_revisions return self.revision_map.get_revisions(id_) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/revision.py", line 298, in get_revisions return sum([self.get_revisions(id_elem) for id_elem in id_], ()) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/revision.py", line 303, in get_revisions for rev_id in resolved_id) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/revision.py", line 303, in <genexpr> for rev_id in resolved_id) File "/root/Envs/RunningFast/lib/python2.7/site-packages/alembic/script/revision.py", line 358, in _revision_for_ident resolved_id) alembic.util.exc.CommandError: Can’t locate revision identified by ‘df80d262d138’ |
后来发现,此处底层内部的数据库:mysql,记录了之前的版本号:
所以估计是:
直接去删除掉数据库中的,就可以了。
再去migrate,的确就可以了:
(RunningFast) ➜ staging python db_manager.py db migrate -m "add user location" <div–<—————————————————————————— DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:73]: app=<Flask ‘runningfast’>, api=<flask_restful.Api object at 0x7f61406aa4d0>, redis_store=<flask_redis.FlaskRedis object at 0x7f6141d1f210>, db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>, server_mode=staging, server_type=develop, rq=<flask_rq2.app.RQ object at 0x7f61406aa790> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:83]: UPDATE_USER_STATUS_INTERVAL_SECONDS=5.0, MAX_OFFLINE_INTERVAL_SECONDS=10.0 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in models [/root/RunningFast/staging/runningfast/models.py:13]: prefix=user-, generatedUuid4Str=bcd00c42-28c8-41dd-8c8b-225716288ad0, newUuid=user-bcd00c42-28c8-41dd-8c8b-225716288ad0 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/root/RunningFast/staging/runningfast/views.py:45]: API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open <div–<—————————————————————————— INFO [alembic.runtime.migration] Context impl MySQLImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. INFO [alembic.autogenerate.compare] Detected added column ‘users.isOnline’ INFO [alembic.autogenerate.compare] Detected added column ‘users.latitude’ INFO [alembic.autogenerate.compare] Detected added column ‘users.longitude’ INFO [alembic.autogenerate.compare] Detected added column ‘users.updateLocationdAt’ Generating /root/RunningFast/staging/migrations/versions/ec2c924d0633_add_user_location.py … done (RunningFast) ➜ staging cat migrations/versions/ec2c924d0633_add_user_location.py |
且对应的文件内容中也可以看出来,新增了几列:
(RunningFast) ➜ staging cat migrations/versions/ec2c924d0633_add_user_location.py """add user location Revision ID: ec2c924d0633 Revises: None Create Date: 2016-10-17 14:54:18.989241 """ # revision identifiers, used by Alembic. revision = ‘ec2c924d0633’ down_revision = None from alembic import op import sqlalchemy as sa def upgrade(): ### commands auto generated by Alembic – please adjust! ### op.add_column(‘users’, sa.Column(‘isOnline’, sa.Boolean(), nullable=False)) op.add_column(‘users’, sa.Column(‘latitude’, sa.Float(), nullable=False)) op.add_column(‘users’, sa.Column(‘longitude’, sa.Float(), nullable=False)) op.add_column(‘users’, sa.Column(‘updateLocationdAt’, sa.DateTime(), nullable=False)) ### end Alembic commands ### def downgrade(): ### commands auto generated by Alembic – please adjust! ### op.drop_column(‘users’, ‘updateLocationdAt’) op.drop_column(‘users’, ‘longitude’) op.drop_column(‘users’, ‘latitude’) op.drop_column(‘users’, ‘isOnline’) ### end Alembic commands ### |
再去upgrade:
(RunningFast) ➜ staging python db_manager.py db upgrade <div–<—————————————————————————— DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:73]: app=<Flask ‘runningfast’>, api=<flask_restful.Api object at 0x7fd806dec4d0>, redis_store=<flask_redis.FlaskRedis object at 0x7fd808460210>, db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>, server_mode=staging, server_type=develop, rq=<flask_rq2.app.RQ object at 0x7fd806dec790> <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:83]: UPDATE_USER_STATUS_INTERVAL_SECONDS=5.0, MAX_OFFLINE_INTERVAL_SECONDS=10.0 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in models [/root/RunningFast/staging/runningfast/models.py:13]: prefix=user-, generatedUuid4Str=91fa4d93-a0ca-4567-a982-ffc70923c3d0, newUuid=user-91fa4d93-a0ca-4567-a982-ffc70923c3d0 <div–<—————————————————————————— <div–<—————————————————————————— DEBUG in views [/root/RunningFast/staging/runningfast/views.py:45]: API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open <div–<—————————————————————————— INFO [alembic.runtime.migration] Context impl MySQLImpl. INFO [alembic.runtime.migration] Will assume non-transactional DDL. INFO [alembic.runtime.migration] Running upgrade -> ec2c924d0633, add user location |
去mysql数据库中确认:
以及:
【总结】
之前由于删掉了:
migrations/versions/
下面对应的用于升级和降级数据库字段的python文件,
导致
db_manager.py db migrate |
出错,
最终通过:
去对应的数据库中,找到:
alembic_version数据库中,删除旧的版本号字段:df80d262d138
重新去
db_manager.py db migrate |
即可。
之后就可以正常的upgrade数据库了。
转载请注明:在路上 » 【已解决】Flask中用Flask-Migrate升级数据库时出错:alembic.util.exc.CommandError Can’t locate revision identified by