On Oct 11, 2008, at 9:41 PM, Jonathan Ellis wrote:
>
> I want to do something like this:
>
> select ticket.*, (select count(*) from ticket_changes where
> ticket_changes.ticket = ticket.id) as count
> from ticket
>
> at the ORM layer. My best stab at it was
>
>>>> subquery = select([func.count('*').label('count')],
>>>> from_obj=ticket_changes)
>>>> session.query(Ticket, subquery.c.count).join(subquery).all()
>
> but, .join seems to want to do an actual join in the FROM clause, and
> I just want it to go into the SELECT clause as a correlated subq. The
> docs don't have any examples of a correlated subquery from the ORM so
> I'm starting to think maybe this isn't possible yet.
a correlated select needs to join to the outer table in the WHERE
clause, so:
query
(Ticket
,select
([func
.count
().label
('count
')]).where(ticket_changes.c.ticket_id==Ticket.id).as_scalar()).all()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---