Error on Flask-migrate with application factory Error on Flask-migrate with application factory flask flask

Error on Flask-migrate with application factory


flask_migrate will attempt to use Manager from flask-script, but only if flask-script is installed.

Simply do, and it should work. You can also import directly from flask_script.

pip install flask_script

manage.py

from flask_migrate import MigrateCommandfrom flask_script import Managerfrom run import create_appapp = create_app()manager = Manager(app)manager.add_command('db', MigrateCommand)


There are two ways to run the Flask-Migrate commands. The newer method uses the Flask CLI, the older uses Flask-Script. Since you don't seem to have Flask-Script installed, I'm going to assume that you intend to use the Flask CLI.

So you need to throw away manage.py since that only applies to Flask-Script. Then move your application variable to the global scope:

db = SQLAlchemy()migrate = Migrate()def create_app():    app = Flask(__name__)    db.app = app    db.init_app(app)    migrate.init_app(app, db)    return appapplication = create_app()if __name__ == '__main__':    application.run()

Then set your FLASK_APP variable:

$ export FLASK_APP=run.py

And now you should be able to run the application with flask run, and the database commands with flask db <command>.