3 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: Mapper with custom o...
FromSent OnAttachments
Koen Bok25 Mar 2007 15:17 
Michael Bayer25 Mar 2007 19:33 
Koen Bok26 Mar 2007 02:41 
Subject:[sqlalchemy] Re: Mapper with custom outerjoin and sum functions
From:Michael Bayer (mike@zzzcomputing.com)
Date:03/25/2007 07:33:26 PM
List:com.googlegroups.sqlalchemy

On Mar 25, 2007, at 6:17 PM, Koen Bok wrote:

stock_request = select( [stock_request_join] + \ [stock_table.c.quantity.op('-') (func.sum(request_table.c.quantity)).label('unordered')] + \ [stock_table.c.quantity.op('-') (func.sum(request_table.c.allocation)).label('unallocated')], group_by=[c for c in stock_request_join.c], correlate=False).alias('stock_request')

This generates the right SQL. But I can't get it to work with the original column names, then I get name ambigious errors.

dont know what a name ambiguous error is (usually thats thrown by postgres directly, meaning the SQL *is* wrong).

When I do it with the joined names, the mapper tries to insert a request too when I want to create stock which makes kind of sense.

When I remove [stock_request_join] and insert [c for c in stock_request_join.c] the JOIN syntax is being removed and it does a regular select against request and stock.

Basically I want to end up with a regular Stock object with the aggregrate columns request_table.c.quantity and request_table.c.allocation. I have it working with a regular select, but if there is no request in te table linked to a stock it does not return that stock when doing a session.query(Stock).select(). Left outer joining request would do the trick.

select([stock_table, request_table.c.quantity, request_table.c.allocation], from_obj=[stock_table.outerjoin (request_table)]) ?