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

db.perform_query fails when called with a string: sqlalchemy not an executable object/no attribute '_execute_on_connection' #159

Open
rct opened this issue Oct 14, 2024 · 1 comment

Comments

@rct
Copy link

rct commented Oct 14, 2024

Calling db.perform_query() with a query in a string results in the following errors:

  • sqlalchemy.exc.ObjectNotExecutableError: Not an executable object: <query string>
  • AttributeError: 'str' object has no attribute '_execute_on_connection'

Tested current master with sqlalchemy==2.0.7 (version from requirements.txt). See #157.

A work around identified in #157 appears to be to use sqlalchemy.text() to convert the query string.

Here is a pytest case test_query.py. The first test succeeds, the 2nd falls.

import detective.core as detective
import detective.functions as functions
import pandas as pd
import sqlalchemy

db_url = "sqlite:///tests/test.db"

def test_query():
    db = detective.HassDatabase(db_url)
    assert db is not None
    assert len(db.entities) > 0

    # Temporary: show perform_query works with sqlalchemy.txt
    df = db.perform_query(sqlalchemy.text("SELECT 1;"));
    assert df is not None

    # a raw string currently doesn't work
    df = db.perform_query("SELECT 1;");
    assert df is not None

If db.perform_query() should accept a string, then it should do what's necessary to prepare it for sqlalchemy's execution.

@rct
Copy link
Author

rct commented Oct 14, 2024

There are reports that the breaking change started in SQLAlchemy 2.0 and impacted other projects (like pandas).

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

No branches or pull requests

1 participant