using flask-migrate with flask-script and application factory using flask-migrate with flask-script and application factory flask flask

using flask-migrate with flask-script and application factory


When you use a --config option you have to use an application factory function as you know, as this function gets the config as an argument and that allows you to create the app with the right configuration.

Since you have an app factory, you have to use the deferred initialization for all your extensions. You instantiate your extensions without passing any arguments, and then in your app factory function after you create the application, you call init_app on all your extensions, passing the app and db now that you have them.

The MigrateCommand is completely separate from this, you can add that to the Manager without having app and db instances created.

Example:

manage.py:

from app import create_appfrom flask_migrate import MigrateCommand, Managermanager = Manager(create_app)manager.add_option("-c", "--config", dest="config_module", required=False)manager.add_command('db', MigrateCommand)

app.py (or however your app factory module is called)

from flask import Flaskfrom flask_sqlalchemy import SQLAlchemyfrom flask_migrate import Migratedb = SQLAlchemy()migrate = Migrate()def create_app(config):    app = Flask(__name__)    # apply configuration    # ...    # initialize extensions    db.init_app(app)    migrate.init_app(app, db)    return app