Python Forum

Full Version: Flask - Implementing SQLAlchemy with Blueprints
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
A bit of a python/flask newb and need some direction with implementing SQLAlchemy with my Blueprint.. follow up question, should I just use Django at this point?

Here is what is in my apps __init__.py file

from flask import Flask, current_app
from PhictitiousBand.api.routes import mod as api_mod
from PhictitiousBand.site.home.routes import mod as home_mod
from PhictitiousBand.site.merchandise.routes import mod as merch_mod
from PhictitiousBand.site.forum.routes import mod as forum_mod
from PhictitiousBand.admin.routes import mod as admin_mod

app = Flask(__name__)

# Create database resources
from PhictitiousBand.admin.models import db as admin_db
current_app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////mnt/C/TEMP/FlaskDB/test.db'
current_app.config['SECRET_KEY'] = 'mysecret'

# Admin app database
admin_db.init_app(app)
with app.app_context():
    admin_db.create_all()

app.register_blueprint(home_mod)
app.register_blueprint(api_mod, url_prefix="/api")
app.register_blueprint(merch_mod, url_prefix='/merchandise')
app.register_blueprint(forum_mod, url_prefix='/forum')

# Admin App for application
app.register_blueprint(admin_mod, url_prefix="/admin")
This is what my blueprint model looks like (really basic model just making sure I can connect everything)
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

class Person(db.Model):
     id = db.Column(db.Integer, primary_key=True)
     name = db.Column(db.String(30))
This is the error I get:

"RuntimeError: Working outside of application context.

This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information."

Here is the repo: https://github.com/jolsendev/PhictitiousBand.git
For anyone who runs into this problem in the future i found a simple solution here: https://stackoverflow.com/questions/9692...45#9695045