flask db init : KeyError 'migrate' in PyCharm


I am running the following code in PyCharm and kepp getting the KeyError ‘migrate’

import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

basedir = os.path.abspath(os.path.dirname(__file__))

app = Flask(__name__)

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+os.path.join(basedir,'data.sqllite')

db = SQLAlchemy(app)
migrate = Migrate(app, db)

I then run set FLASK_APP=app_database.py and flask db init in command line but I keep getting the directory = current_app.extensions[‘migrate’].directory
KeyError: ‘migrate’

I am stuck. Can anybody help?
Thank you very much!


Adding migrate = Migrate(app, db) into init.py resolved the issue. (I didn’t realize this was necessary for the cli functionality and database initialization, assumed it was related only to database migrations afterwards.)

Running flask db init now works correctly:

Answered By – Alen John

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published