Get Bind Fetch from ORM query

I am trying to use pandas-sqlalchemy-pivot to create pivot tables based on the results of an ORM SQLAlchemy query.

The functions of this module (for example pivots.pivot_table_from_select

) are designed to work with SQLAlchemy basic selections , not ORM queries.

I assumed I could use the Query.selectable attribute to convert the ORM query to the main selectable, but that gives an error:

>>> q=session.query( ... )
>>> df = pivots.pivot_table_from_select(q.selectable(), rows="field", cols="state", values="count")
  File "...\site-packages\pivots\table.py", line 25, in pivot_table_from_select
    data = pivot_data(select, rows, cols, values)
  File "...\site-packages\pivots\table.py", line 74, in pivot_data
    data = pivot_select.execute(bind=select.bind).fetchall()
  File "...\lib\site-packages\sqlalchemy\sql\base.py", line 385, in execute
    raise exc.UnboundExecutionError(msg)
sqlalchemy.exc.UnboundExecutionError: This Select object is not directly bound to a Connection or Engine.Use the .execute() method of a Connection
Engine to execute this construct.

      

I can't figure out why this object is unbound, since the original object Query

is definitely associated with a specific session and engine.

I get the same error if I try to execute the request, even with explicit binding:

>>> q.selectable.execute(bind=engine)

      

... but if I do this, it works:

>>> engine.execute(q.selectable)

      

I would like to be able to use an unmodified module pandas-sqlalchemy-pivot

; is there a way to get the binding being fetched from the request object that I am missing?

+3


source to share





All Articles