Python Forum

Full Version: Flask migrate sqlalchemy not found
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Good day,

I am trying to make migration via Flask/Migrate/SQLAlchemy and getting the following error:
    'The sqlalchemy extension was not registered to the current ' \
AssertionError: The sqlalchemy extension was not registered to the current application.  Please make sure to call init_app() first.
What could be the reason?

My run.py file is inside project folder. run.py contains code:
from flaskblog import app, db
from flask import Flask

db.init_app(app)
if __name__ == '__main__':
	app.run(debug=True, port=5002)
	socketio.run(app)
Inside project folder there is flaskblog folder. Then __init__ file there contains code:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
#... other packages..

basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SECRET_KEY'] = 'dfjkfjkfdjklfjlkfdlfdlkj'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////home/tomas/searvice/flaskblog/site.db'
#...

db = SQLAlchemy(metadata=MetaData(naming_convention=naming_convention))

migrate = Migrate(app, db)
migrate.init_app(app, db, render_as_batch=True)

#...
So in terminal inside project folder I was not sure how to set
FLASK_APP=WhatToTypeHere?"
Instead I wrote command
export FLASK_APP=flaskblog
 flask db init
worked, but
flask db migrate 
returns the error I have mentioned. Cry
Miguel Grinberg goes over this in detail.
I can't point out the exact location in Miguel Grinberg's tutorial on migrations, but it's somewhere around here:
https://blog.miguelgrinberg.com/post/the...v-database
Heart
Error was in this line:

db = SQLAlchemy(metadata=MetaData(naming_convention=naming_convention))
should be db =SQLAlchemy(app)