handling the results of `session.execute`

75 views
Skip to first unread message

Jonathan Vanasco

unread,
Feb 5, 2016, 5:32:33 PM2/5/16
to sqlalchemy
I've run into a few cases where I need to abandon the ORM and run `session.execute()` 

Usually... I'm doing a bunch of nested queries and only pull out a `count` or a few rows of 1-2 id columns. Writing in pure sql is faster (for me), gives me more control, and avoids having to do a baked-query.

Here's where my concern comes in -- `execute` returns a ResultProxy, and the first element is a RowProxy

so to get a count, I'm doing something like this:

    result = dbSession.execute(foo)
    result = list(result)[0][0]

This is... ugly.  and then I have to handle the logic to ensure I got a correct record back.

I'm wondering if anyone has figured a more elegant way to handle queries like this.

The best i can think of is using an shared function:

    result = extract_result_one(result)
    result = extract_result_first(result)
    result = extract_result_count(result)

and then just raise an appropriate error if there are too many (or no) rows.

anyone have a better idea?

Mike Bayer

unread,
Feb 5, 2016, 6:03:11 PM2/5/16
to sqlal...@googlegroups.com


On 02/05/2016 05:32 PM, Jonathan Vanasco wrote:
> I've run into a few cases where I need to abandon the ORM and run
> `session.execute()`

horrors

>
> Usually... I'm doing a bunch of nested queries and only pull out a
> `count` or a few rows of 1-2 id columns. Writing in pure sql is faster
> (for me), gives me more control, and avoids having to do a baked-query.
>
> Here's where my concern comes in -- `execute` returns a ResultProxy, and
> the first element is a RowProxy
>
> so to get a count, I'm doing something like this:
>
> result = dbSession.execute(foo)
> result = list(result)[0][0]
>
> This is... ugly. and then I have to handle the logic to ensure I got a
> correct record back.

why not call result.scalar() ?


>
> I'm wondering if anyone has figured a more elegant way to handle queries
> like this.
>
> The best i can think of is using an shared function:
>
> result = extract_result_one(result)
> result = extract_result_first(result)
> result = extract_result_count(result)
>
> and then just raise an appropriate error if there are too many (or no) rows.
>
> anyone have a better idea?

result.scalar() I think is what you're looking for



>
> --
> 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
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Jonathan Vanasco

unread,
Feb 5, 2016, 6:49:58 PM2/5/16
to sqlalchemy


On Friday, February 5, 2016 at 6:03:11 PM UTC-5, Michael Bayer wrote:

horrors

i know :/
 

why not call result.scalar() ?

ignorance.
 

result.scalar() I think is what you're looking for

yep!


Reply all
Reply to author
Forward
0 new messages