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

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

Reply via email to