flask db init leads to "KeyError: 'migrate'

I created already a database and added 2 tables (venue, artist).

This is a party from my app.py:

import json
import dateutil.parser
import babel
from flask import Flask, render_template, request, Response, flash, redirect, url_for
from flask_moment import Moment
from flask_sqlalchemy import SQLAlchemy
import logging
from logging import Formatter, FileHandler
from flask_wtf import Form
from forms import *
from flask_migrate import Migrate
#----------------------------------------------------------------------------#
# App Config.
#----------------------------------------------------------------------------#
app = Flask(__name__)
moment = Moment(app)
app.config.from_object('config')
db = SQLAlchemy(app)
# TODO: connect to a local postgresql database
##Done via the config file
#----------------------------------------------------------------------------#
# Models.
#----------------------------------------------------------------------------#
#Migrate
migrate =(app,db)
class Venue(db.Model):
    __tablename__ = 'venue'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    city = db.Column(db.String(120))
    state = db.Column(db.String(120))
    address = db.Column(db.String(120))
    phone = db.Column(db.String(120))
    image_link = db.Column(db.String(500))
    facebook_link = db.Column(db.String(120))
    # TODO: implement any missing fields, as a database migration using Flask-Migrate
class Artist(db.Model):
    __tablename__ = 'artist'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    city = db.Column(db.String(120))
    state = db.Column(db.String(120))
    phone = db.Column(db.String(120))
    genres = db.Column(db.String(120))
    image_link = db.Column(db.String(500))
    facebook_link = db.Column(db.String(120))

In my config file I create the connection.

If I try to run "flask db init" in the directory where also my app.py and all the other files are located I get the following error:

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

KeyError: 'migrate'

I tried it for hours know but found no solution so far. What am I doing wrong? Please help me.


I think you should add the code below:

migrate = Migrate(app, db) # this

For further info you can check this link


Flask comand uses env variable FLASK_APP, to know where Flask app is located. For your application you will have to set FLASK_APP=microblog.py, as goes in chapter 1.

source - https://www.kickstarter.com/projects/1124925856/the-new-and-improved-flask-mega-tutorial)

RUS - Помните, что flask команда полагается на переменную среды FLASK_APP, чтобы знать, где расположено приложение Flask. Для этого приложения вы хотите установить FLASK_APP=microblog.py, как описано в главе 1.