I'm in the process of switching my application to use Flask-SQLAlchemy, from
the straight SQL style of the example "flaskr" application.
in the quickstart tutorial:
http://packages.python.org/Flask-SQLAlchemy/quickstart.html#a-minimal-application
I notice that db is a global variable, along with app. If we were to
introduce SQLAlchemy into a simple application like flaskr, is the proper
way to do something like:
@app.before_request
def before_request():
g.db = SQLAlchemy(app)
or does it not matter, and should I just have "db" as a global variable
which is initialized alongside app?
also, I was looking at some different documentation on SQLAlchemy in the
"patterns for flask" section:
http://flask.pocoo.org/docs/patterns/sqlalchemy/
and noticed that it says session.remove() needs to be called when finished.
however, the Flask-SQLAlchemy docs don't say this, so I'm curious if it's
handled by the extension? if I am using the extension, does the "patterns
for flask" docs really apply at all?
thanks for your help.
--paul
Hi, On 7/24/11 4:03 PM, Paul Sanwald wrote: > I notice that db is a global variable, along with app. If we were to > introduce SQLAlchemy into a simple application like flaskr, is the > proper way to do something like: > @app.before_request > def before_request(): > g.db = SQLAlchemy(app) Nope, that is not a good idea because it would reinitialize SQLAlchemy each request. That `db` object that Flask-SQLAlchemy has there is already doing the before request handling for you. > or does it not matter, and should I just have "db" as a global variable > which is initialized alongside app? Global variable it is. > and noticed that it says session.remove() needs to be called when > finished. however, the Flask-SQLAlchemy docs don't say this, so I'm > curious if it's handled by the extension? if I am using the extension, > does the "patterns for flask" docs really apply at all? Does not apply at all. The purpose of Flask-SQLAlchemy is to do all that for you automatically. You don't have to deal with that at all :) Regards, Armin
Cool, thanks Armin --paul On Jul 24, 2011, at 10:49 AM, Armin Ronacher <armin.ronacher@active-4.com> wrote: > Hi, > > On 7/24/11 4:03 PM, Paul Sanwald wrote: >> I notice that db is a global variable, along with app. If we were to >> introduce SQLAlchemy into a simple application like flaskr, is the >> proper way to do something like: >> @app.before_request >> def before_request(): >> g.db = SQLAlchemy(app) > Nope, that is not a good idea because it would reinitialize SQLAlchemy > each request. That `db` object that Flask-SQLAlchemy has there is > already doing the before request handling for you. > >> or does it not matter, and should I just have "db" as a global variable >> which is initialized alongside app? > Global variable it is. > >> and noticed that it says session.remove() needs to be called when >> finished. however, the Flask-SQLAlchemy docs don't say this, so I'm >> curious if it's handled by the extension? if I am using the extension, >> does the "patterns for flask" docs really apply at all? > Does not apply at all. The purpose of Flask-SQLAlchemy is to do all > that for you automatically. You don't have to deal with that at all :) > > > Regards, > Armin