Battleroid Battleroid - 9 months ago 89
Python Question

Flask and SQLAlchemy, application not registered on instance

I'm currently trying to piece together a small Flask application. This is my structure.
contains just the SQLAlchemy object:

db = SQLAlchemy()

I then import this into my
to create my models. Lastly, inside
I import
and do:

from .database import db
from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///application.db'

However, I cannot create the tables from the models it appears. If I remove
. The application will run with no problems, but obviously the database is not created. When
is present I am given 'RuntimeError: application not registered on db instance and no application bound to current context'.

I'm honestly confused, as before I was having issues just starting the application without creating the database, but moving
to it's own file seems to somehow have corrected that issue. Now, the only issue remains is actually creating the database.

Can anyone tell me what might be the problem? I'm genuinely stumped.

Answer Source

The answer is here:

See the part about:

The difference between the two is that in the first case methods like create_all() and drop_all() will work all the time but in the second case a flask.Flask.request_context() has to exist.

There's more information here:

If all that is confusing (it probably is, since it's talking about a fairly advanced feature of Flask), the short short version is db.init_app(app) changes the app object, but it doesn't change anything in the db object. It's on purpose because there might be more than one app flying around, and db might have to talk to all of them. (I said it was an advanced feature.)

So when you call db.create_all() without having a request live (which creates a global that has the currently running app) it doesn't know what to connect to, and bombs. That's what the error means.

In your case, I would put the SQLAlchemy call back in and pass app to it, that's the easiest way:

db = SQLAlchemy(app)

Or keep things as they are, and run the setup before the first request:

def create_database():

I hope that helps! Let me know if you run into any more problems.