Flask2でのFlask-Migrate

スポンサードサーチ

事象

下記記事の時は、Flask v1 でFlask-Migrateを使っていました。

migrationを実行する際は、Flask-ScriptとFlask-MigrateのMigrateCommandを使用していたけれど、

$ python manage.py db upgrade

Flask を v2に上げたら、以下のようなエラーが出て使えなくなっていた。

ModuleNotFoundError: No module named 'flask._compat'
ImportError: cannot import name 'MigrateCommand' from 'flask_migrate' (/usr/local/lib/python3.11/site-packages/flask_migrate/__init__.py)

どうやら、Flask-ScriptはFlask v2には対応していないらしい。

対応

単にFlask-Scriptを使わずに、下記で行えばいい。

$ flask db upgrade

ただ、flask db コマンドはパスによっては使えないので、各自の設定を確認してみること。
例えば、私が作成しているサンプルでは、flaskrディレクトリ上でないと使えないです。

flask –help の Commands:の一覧にdbが表示されるかで確認できます

root@3a5e0b4944fd:/var/www/flaskr# flask --help
Usage: flask [OPTIONS] COMMAND [ARGS]...

  A general utility script for Flask applications.

  An application to load must be given with the '--app' option, 'FLASK_APP'
  environment variable, or with a 'wsgi.py' or 'app.py' file in the current
  directory.

Options:
  -e, --env-file FILE   Load environment variables from this file. python-
                        dotenv must be installed.
  -A, --app IMPORT      The Flask application or factory function to load, in
                        the form 'module:name'. Module can be a dotted import
                        or file path. Name is not required if it is 'app',
                        'application', 'create_app', or 'make_app', and can be
                        'name(args)' to pass arguments.
  --debug / --no-debug  Set debug mode.
  --version             Show the Flask version.
  --help                Show this message and exit.

Commands:
  db      Perform database migrations.
  routes  Show the routes for the app.
  run     Run a development server.
  shell   Run a shell in the app context.

root@3a5e0b4944fd:/var/www/flaskr# cd ../
root@3a5e0b4944fd:/var/www# flask --help
Usage: flask [OPTIONS] COMMAND [ARGS]...

  A general utility script for Flask applications.

  An application to load must be given with the '--app' option, 'FLASK_APP'
  environment variable, or with a 'wsgi.py' or 'app.py' file in the current
  directory.

Options:
  -e, --env-file FILE   Load environment variables from this file. python-
                        dotenv must be installed.
  -A, --app IMPORT      The Flask application or factory function to load, in
                        the form 'module:name'. Module can be a dotted import
                        or file path. Name is not required if it is 'app',
                        'application', 'create_app', or 'make_app', and can be
                        'name(args)' to pass arguments.
  --debug / --no-debug  Set debug mode.
  --version             Show the Flask version.
  --help                Show this message and exit.

Commands:
  routes  Show the routes for the app.
  run     Run a development server.
  shell   Run a shell in the app context.

もし、デプロイと同時に migration を行いたい場合は、
Dockerfile の WORKDIR を flask db コマンドが実行できるパスにするといいかもしれないです。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です