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

sql_select/sql_explain not working with Flask-SQLAlchemy 3 & SQLAlchemy 2 #232

Open
taoky opened this issue Feb 7, 2024 · 2 comments
Open

Comments

@taoky
Copy link

taoky commented Feb 7, 2024

sql_select() (panels/sqlalchemy.py) seems not tested in #186. The updates introduce some API changes that affect how sql_select() operates:

  • get_engine() requires an initialized (init_app()ed) SQLAlchemy instance.
  • The Engine object no longer has an execute() method directly; instead, obtaining a connection is required for execution.
  • The execute() method does not support positional parameters anymore.

To address these issues, I made some local modifications. Here are the specific changes:

142c142,143
<     engine = SQLAlchemy().get_engine(current_app)
---
>     sqlalchemy = current_app.extensions["sqlalchemy"]
>     engine = sqlalchemy.get_engine()
150c151,152
<     result = engine.execute(statement, params)
---
>     with engine.connect() as connection:
>         result = connection.exec_driver_sql(statement, tuple(params[0]))

Given that I'm uncertain about the repercussions these changes might have on backward compatibility with Flask-SQLAlchemy 2.x versions, and due to time constraints on my end, I'm hesitant to submit a pull request myself. However, it would be great if someone could validate and extend these adjustments to ensure compatibility across both Flask-SQLAlchemy 2.x and 3.x versions.

@macnewbold
Copy link
Contributor

Thanks for reporting it, and providing some code!

I agree that it may need some attention and testing to make a change that would support both the old and the new, but with the start you've given above, it's probably within relatively easy reach when someone has time. Just need to identify the right condition for an if/else in those two places, and we'd probably be set I would hope.

@qpleple
Copy link

qpleple commented Oct 8, 2024

I confirm the findings of @taoky: the sql_select/sql_explain features don't work anymore.

image

Clicking on SELECT throws this exception:

  File ".../flask_debugtoolbar/panels/sqlalchemy.py", line 177, in sql_select
    result = engine.execute(statement, params)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Engine' object has no attribute 'execute'

Indeed, in SQLAlchemy 2, the engine doesn't have an execute() method anymore and we need to use sessions as proposed by @taoky.

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

No branches or pull requests

3 participants