The title says it all. Here is the code snippet.
async with EngineContext(uri=URI) as engine:
session = async_sessionmaker(bind=engine, expire_on_commit=True)()
async with session.begin():
stmt: Select = select(User).order_by(_GenerativeSelect__first=User.login_date.desc()).limit(limit=10)
result = await session.execute(statement=stmt)
Equivalent to the very simple query,
SELECT * FROM User ORDER BY login_date DESC LIMIT 10;
The query is working fine and I am getting the results as expected. But I want to form a polars dataframe from the result, and without having to hardcode (or externally supply) the column names. I cannot seem to get the correct public APIs of the result object to manage it. When I try result.keys()
, I get RMKeyView(['User'])
, not the column names.
I tried iterating over the results, where each object has the column names as attributes, but there are many other attributes. So the code cannot be dynamic enough to pick up the column names.
So any help on the cleanest way to form a polars Dataframe (preferably lazy) from this result?
Related, when I begin the session, is there a way to explicitly mark the session as read-only in SQL alchemy, to preclude any possibility of data corruption when no write is intended in that session?
2
Answers
After some pain, I got this piece of rather convoluted mess working, but I believe there should be a cleaner way to do it.
Van's solution fails because apparently, polars and aiomysql driver do not play well with each other.