Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLAlchemy 0 queries on swagger #234

Open
copyNdpaste opened this issue Mar 27, 2024 · 0 comments
Open

SQLAlchemy 0 queries on swagger #234

copyNdpaste opened this issue Mar 27, 2024 · 0 comments

Comments

@copyNdpaste
Copy link

copyNdpaste commented Mar 27, 2024

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 #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 []

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant