ORM query returning separate columns instead of objects

6 views
Skip to first unread message

Gord Thompson

unread,
Aug 31, 2021, 5:01:08 PMAug 31
to sqlalchemy
My memory is failing me. I seem to recall that there was a succinct way to get a 1.4/2.0 ORM query to return individual columns instead of ORM objects. That is, to tell this

print(session.execute(select(User)).fetchall())
# [(<__main__.User object at 0x00000090175EC700>,)]

to return the equivalent of this

print(session.execute(select(User.id, User.name)).fetchall())
# [(1, 'Gord')]

without explicitly naming each attribute (column). A modifier like `.as_columns()` or something like that …?

Interestingly, if I try to use `*` I only get the first (or maybe the PK) column

print(session.execute(select(text("*")).select_from(User)).fetchall())
# [(1,)]

Mike Bayer

unread,
Sep 1, 2021, 9:49:06 AMSep 1
to noreply-spamdigest via sqlalchemy
you can take the statement and execute it on the connection instead, then you'll get tuples with columns.  not sure if there was some way to get this through session.execute() directly.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages