Issue
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__))
print(basedir)
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+os.path.join(basedir,'data.sqllite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
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’ error.
I am stuck. Can anybody help?
Thank you very much!
Solution
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