Flask-SQLAlchemy import/context issue Flask-SQLAlchemy import/context issue flask flask

Flask-SQLAlchemy import/context issue


The flask_sqlalchemy module does not have to be initialized with the app right away - you can do this instead:

# apps.members.modelsfrom flask_sqlalchemy import SQLAlchemydb = SQLAlchemy()class Member(db.Model):    # fields here    pass

And then in your application setup you can call init_app:

# apps.application.pyfrom flask import Flaskfrom apps.members.models import dbapp = Flask(__name__)# later ondb.init_app(app)

This way you can avoid cyclical imports.

This pattern does not necessitate the you place all of your models in one file. Simply import the db variable into each of your model modules.

Example

# apps.shared.modelsfrom flask_sqlalchemy import SQLAlchemydb = SQLAlchemy()

# apps.members.modelsfrom apps.shared.models import dbclass Member(db.Model):    # TODO: Implement this.    pass

# apps.reporting.membersfrom flask import render_templatefrom apps.members.models import Memberdef report_on_members():    # TODO: Actually use arguments    members = Member.filter(1==1).all()    return render_template("report.html", members=members)

# apps.reporting.routesfrom flask import Blueprintfrom apps.reporting.members import report_on_membersreporting = Blueprint("reporting", __name__)reporting.route("/member-report", methods=["GET","POST"])(report_on_members)

# apps.applicationfrom flask import Flaskfrom apps.shared import dbfrom apps.reporting.routes import reportingapp = Flask(__name__)db.init_app(app)app.register_blueprint(reporting)

Note: this is a sketch of some of the power this gives you - there is obviously quite a bit more that you can do to make development even easier (using a create_app pattern, auto-registering blueprints in certain folders, etc.)


an original app.py: https://flask-sqlalchemy.palletsprojects.com/en/2.x/quickstart/

...app = flask.Flask(__name__)app.config['DEBUG'] = Trueapp.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/test.db'db = flask.ext.sqlalchemy.SQLAlchemy(app)class Person(db.Model):    id = db.Column(db.Integer, primary_key=True)...class Computer(db.Model):    id = db.Column(db.Integer, primary_key=True)...# Create the database tables.db.create_all()...# start the flask loopapp.run()

I just splitted one app.py to app.py and model.py without using Blueprint. In that case, the above answer dosen't work. A line code is needed to work.

before:

db.init_app(app)

after:

db.app = appdb.init_app(app)

And, the following link is very useful.

http://piotr.banaszkiewicz.org/blog/2012/06/29/flask-sqlalchemy-init_app/