Python Forum
Flask - Implementing SQLAlchemy with Blueprints
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask - Implementing SQLAlchemy with Blueprints
#1
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
Reply
#2
For anyone who runs into this problem in the future i found a simple solution here: https://stackoverflow.com/questions/9692...45#9695045
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) database is locked pythonpaul32 1 2,106 Apr-04-2023, 07:44 AM
Last Post: Larz60+
  Flask error sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint pythonpaul32 2 3,645 Feb-21-2023, 03:13 AM
Last Post: noisefloor
  Flask and SQLAlchemy question: Database is being created but tables aren't adding pythonpaul32 3 4,647 Feb-07-2023, 10:48 AM
Last Post: pythonpaul32
  Error updating one to many relationship in Flask/ SQLAlchemy atindra 0 3,334 Apr-15-2021, 10:29 PM
Last Post: atindra
  Flask migrate sqlalchemy not found TomasAm 2 3,487 Dec-01-2020, 10:04 AM
Last Post: TomasAm
  python 3.7 on windows using flask and flask-sqlalchemy. Alpy 2 3,997 Aug-12-2020, 07:24 PM
Last Post: Alpy
  Flask-Sqlalchemy count products in specific category imawesome 2 28,331 Mar-12-2020, 08:14 PM
Last Post: imawesome
  Flask-SqlAlchemy - Relationship failed to locate a name - Please Help howardrwb 0 5,196 Jan-31-2020, 08:38 PM
Last Post: howardrwb
  flask-SQLAlchemy query with keyword as function argument pascale 2 3,477 Mar-13-2019, 08:45 PM
Last Post: Ecniv
  web.py: implementing a captcha kintarowonders 0 2,222 Feb-24-2019, 02:40 AM
Last Post: kintarowonders

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020