0

I'm trying to read data into a DataFrame using read_sql and a SQLAlchemy query object. I'm getting zero rows returned, even though there's data in the database. Further, if I run the query with query.all(), then read_sql returns data.

For a concrete example

query = session.query(SomeSqlAlchemyModel)
# query.all()  # If I uncomment this line, I get data back. Without it I get an empty DataFrame.
df = pd.read_sql(query.statement, session.connection())
assert not df.empty

I'm logging out the SQL statements, and I've stepped into the SQLAlchemy code and from what I can tell, the exact same query is being executed in both scenarios.

How can I get read_sql to return data without running query.all() first?

1 Answer 1

1

I found the problem—I was inserting data into the database using the same session, and I hadn't called flush yet. The query.all() statement implicitly called a flush for me, persisting the data first.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.