pallets-eco / flask-debugtoolbar

A toolbar overlay for debugging Flask applications
https://flask-debugtoolbar.readthedocs.io
BSD 3-Clause "New" or "Revised" License
953 stars 146 forks source link

SQLAlchemy 0 queries on swagger #234

Open copyNdpaste opened 8 months ago

copyNdpaste commented 8 months ago

Im using flask=1.1.2, sqlalchemy=1.4.4 I want to use flask debug toolbar so I installed flask-debugtoolbar = "^0.14.1" flask-sqlalchemy = "2.5.0"

Why sqlalchemy query not recored? Because Im using sqlalchemy such as from sqlalchemy import Column not from flask_sqlalchemy import SQLAlchemy?

I alread read https://github.com/pallets-eco/flask-debugtoolbar/issues/78 but app.config["SQLALCHEMY_RECORD_QUERIES"] = True and SQLALCHEMY_RECORD_QUERIES = True on config are not solution..

Is it possible to use debug toolbar on swagger docs page?

I ran server with docker compose.

image image


base.py

from flask_sqlalchemy import SQLAlchemy

from sqlalchemy.ext.declarative import declarative_base
from flask_debugtoolbar import DebugToolbarExtension

Base = SQLAlchemy(model_class=declarative_base()).Model

def create_base(app):
    db = SQLAlchemy(app, model_class=declarative_base())
    Base = db.Model

    app.debug = True

    DebugToolbarExtension(app)

    db.init_app(app)

    return Base

model.py

from app.main.base import Base
from sqlalchemy import Column

class A(Base):
  id = Column(
        Integer,
       ...
    )

__init__.py

def create_app(config_name):
    app = Flask(__name__)

    app.wsgi_app = ProxyFix(app.wsgi_app)
    app.wsgi_app = PrefixMiddleware(app.wsgi_app, prefix=prefix)
    app.config.from_object(config_by_name[config_name])

    app.config["SECRET_KEY"] = "SECRET_FOR_DEBUG"
    app.config["SQLALCHEMY_DATABASE_URI"] = connect_url
    app.config["DEBUG_TB_ENABLED"] = True
    app.config["DEBUG_TB_PROFILER_ENABLED"] = True

    app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
    app.config["SQLALCHEMY_RECORD_QUERIES"] = True
    app.config["SQLALCHEMY_ECHO"] = True
    app.config["DEBUG_TB_INTERCEPT_REDIRECTS"] = False

    Base = create_base(app)

    bcrypt.init_app(app)
    _init_errorhandler(app)

    app.db_session = ssession
    Base.query = app.db_session.query_property()

    return app

mysql.py

import pymysql

from app.database.constants import MYSQL_SERVER_CONF as MSC

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, scoped_session
from flask import _app_ctx_stack

connect_url = rf"mysql+pymysql://{MSC['user']}:{MSC['password']}@{MSC['host']}:{MSC['port']}/{MSC['db']}?charset=utf8mb4"

engine = create_engine(
    connect_url,
...
)

ssession = scoped_session(
    sessionmaker(autocommit=False, autoflush=False, bind=engine),
    scopefunc=_app_ctx_stack.__ident_func__,
)

session.py

from flask import current_app
from werkzeug.local import LocalProxy
from sqlalchemy.orm import Session

def _get_session() -> Session:
    session = current_app.db_session()
    return session

current_db_session: Session = LocalProxy(_get_session)

repository.py

current_db_session.query(A).all()

from flask_sqlalchemy import get_debug_queries

info = get_debug_queries()
print(info)

but info is []