On Thursday 19 June 2008 14:05:08 ml wrote: > Hi! > > Let's have a typical schema: Company <- Employee > > How can I add a scalar column (in a query or a mapper) to the > Company containing the employees count? E.g. query(Company).all() > will return list of Company instances and each will have an extra > property with emloyees count. But I don't want that retrieved by an > extra query for each company. I want that that in one query like > "SELECT companies.*, count(employees.*) ...".
do u want it once per query or u want it regularly updated/cached? first one, u can add column expressions to the query http://www.sqlalchemy.org/docs/04/mappers.html#advdatamapping_mapper_expressions which would be ok if u dont do it frequently. second one, there is SQLAlchemy Aggregator: http://dev.gafol.net/t/aggregator (there's docs + package + svn) or my version in svn co https://dbcook.svn.sourceforge.net/svnroot/dbcook/trunk/dbcook/misc/aggregator we havent merged long ago, may be there are some small differences (e.g. i've added 0.5 support to dbcook's copy) example: see it's tests or https://dbcook.svn.sourceforge.net/svnroot/dbcook/trunk/dbcook/misc/aggregator4dbcook_example.py have fun svilen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---