jpsca / sqla-wrapper

A friendly wrapper for modern SQLAlchemy and Alembic
https://sqla-wrapper.scaletti.dev
MIT License
150 stars 31 forks source link

Flask debug toolbar doesnt show queries(shows unavailable) #7

Closed ev-agelos closed 9 years ago

ev-agelos commented 9 years ago

As i am trying to make the "transition" from Flask-SQLAlchemy to your fork i found this problem where the debug toolbar doesnt show information about the queries, it just tells me that i have to install Flask-SQLAlachemy.

I saw a commit about supporting the toolbar in your fork but im not expert to debug what is wrong cause maybe im missing something.

# __init__.py
from flask import Flask
from sqlalchemy_wrapper import SQLAlchemy
from flask.ext.bcrypt import Bcrypt

app = Flask(__name__, instance_relative_config=True, static_url_path='')
bcrypt = Bcrypt(app)

app.config.from_pyfile('development.py')

db = SQLAlchemy(app.config['SQLALCHEMY_DATABASE_URI'], app=app)
# run.py
from flask_debugtoolbar import DebugToolbarExtension
toolbar = DebugToolbarExtension()
toolbar.init_app(app)
ghost commented 9 years ago

I'm sorry, this should be on the documentation.

In order to see the queries on the Flask debug toolbar, you must activate first the query recording using the argument record_queries:

db = SQLAlchemy(SQLALCHEMY_URI, app=app, record_queries=True)

(In Flask-SQLAlchemy this is done automatically if DEBUG is True... I'll probably do something similar soon) Here is a functional (but boring) example:

"""
    pip install flask, sqlalchemy_wrapper, flask_debugtoolbar
"""
from flask import Flask
from sqlalchemy_wrapper import SQLAlchemy

app = Flask(__name__)
app.config['DEBUG'] = True
app.config['SECRET_KEY'] = 'loremipsumsitamet'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False

db = SQLAlchemy(
    app.config['SQLALCHEMY_DATABASE_URI'],
    record_queries=app.config['DEBUG'],
    app=app
)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode)

@app.route("/")
def hello():
    db.query(User).all()
    return "<html><body>Hello World!</body></html>"

db.create_all()
db.add(User(name=u'Test'))
db.commit()

if __name__ == "__main__":

    if app.config['DEBUG']:
        import flask_debugtoolbar
        flask_debugtoolbar.DebugToolbarExtension(app)
    app.run()
ghost commented 9 years ago

BTW, although SQLAlchemy-Wrapper is kind-of framework-independent this integration only works with Flask and Flask-DebugToolbar :(