Flask-Migrate: flask db init fails: KeyError 'migrate'

Hi Miguel!

I’m attempting to use your flask-migrate CLI tool as per your tutorial.

Issue: Running flask db init from the terminal fails with error:

    directory = current_app.extensions['migrate'].directory
KeyError: 'migrate' 

Desired result: flask db init should create the database (sqlite) and initialize database tables from all models.

Context:

  1. I’ve exported FLASK_APP='run.py', where run.py does the import of ‘app’ and ‘db’ from (myapp)/__init__.py
  2. Flask app is created with: app = Flask(APP_NAME) in run.py
  3. DB object is created with: db = SQLAlchemy(app) in run.py

Speculative analysis: It seems that current_app.extensions is not somehow being populated with a desired key migrate, which I assume refers to your Flask extension.

Where/how is that supposed to happen?

There may be steps here I’ve missed or required parameters I’m unaware of. Can you point me in the right direction?

Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 25 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Hi fieldse. Did you create a Migrate object? I get the same error when trying flask db init without that object.

from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db) # this

@utsushiiro You’re a wizard. 😄 @miguelgrinberg Correct, that was it.

Yeah, I had abstracted the migrate = Migrate(app, db) to a separate function.

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:

Thanks guys!

Yes, I think @utsushiiro is right, you did not initialize the Flask-Migrate extension by creating a Migrate object.

@phstrauss this line in app/init.py:

migrate = Migrate(db, app)

should be:

migrate = Migrate(app, db)

I have a manage.py in the project base directory, and init the migrations by run

python manage.py db init

@Nerevarishe does it still do it if you remove the app.run() call in app/__init__.py. If you are going to use the Flask CLI you have to start the app with flask run, don’t use app.run().

@malay95 if you project on Win Machine try to go with PowerShell to \venv foulder of Project, (install again all packadges (now we installing not for venv enviroment only) WTF and etc.). then type: $env:FLASK_APP = "<main_file_where_app.run()>.py" then use flask commands: flask db init

@miguelgrinberg I Have changed the init code to this: import os from flask import Flask, request, current_app from config import Config from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate

db = SQLAlchemy() migrate = Migrate()

def create_app(test_config=None): app = Flask(name, instance_relative_config=True) app.config.from_object(Config)

db.init_app(app)
migrate.init_app(app, db)

And I no longer have the error! Many thanks for your help and most of all for your excellent tutorials and hard work with python.