You can call query.subquery().as_scalar() for now.    My problem with 
"as_scalar()" is that I hate the name, but "scalar()" is already taken.  



On Sep 1, 2010, at 1:32 AM, Russell Warren wrote:

> Another question on this...
> 
> Is there any particular reason that orm.query objects don't have
> as_scalar methods?
> 
> While trying to hack around and figure out how to make this work, one
> thing I definitely tried was substituting one-item-result queries in
> as scalar replacement values in a similar fashion to the select you
> showed.  It did not work because Query objects are not accepted as
> binding parameters even if they only have one column.  If there were
> an equivalent "as_scalar" call on a query as what exists for the
> Select object, it seems like this type of scalar variable substitution
> could be cleanly done and remain in the ORM frame of mind.
> 
> To demonstrate the obvious equivalence, below is a small comparison...
> 
>>>> s = select([User.id]).where(User.name == 'jack')
>>>> q = sess.query(User.id).filter(User.name == "jack")
>>>> print s
> SELECT users.id
> FROM users
> WHERE users.name = :name_1
>>>> print q
> SELECT users.id AS users_id
> FROM users
> WHERE users.name = :name_1
>>>> s.as_scalar()
> <sqlalchemy.sql.expression._ScalarSelect at 0x1a40a90; Select object>
>>>> q.as_scalar()
> Traceback (most recent call last):
>  File "<string>", line 1, in <fragment>
> AttributeError: 'Query' object has no attribute 'as_scalar'
> 
> 
> If queries had as_scalar(), the equivalent way to add an address with
> one query would just be...
> 
> uid = sess.query(User.id).filter(User.name == "jack").as_scalar()
> sess.add(Address(user_id = uid,
>         email_address = 'blah'))
> 
> which is 100% readable and logical to me, at least from a usage
> perspective.  You would just need to know about the as_scalar method.
> 
> Having said that, the more I look at the combo ORM+select way, that is
> also quite logical and readable.  I just need to burn into my mind
> that the base sql toolkit and orm work really nicely together and that
> I don't necessarily need to stick to one side of the fence.
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalch...@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to