On May 27, 2007, at 4:23 PM, SamDonaldson wrote:
> > How do I look to see what sql is being constructed in the query > object? So if I have something like: > > q = query.session(User) > q = q.add_column(....) > q = q.group_by(.....).select() > > How do I check what sql string has been constructed in q? the Select object is returned by query.compile(). but also you can just enable logging of 'sqlalchemy.engine' or use echo=True on create_engine() which may be more expedient. > > Also, say I want to do a group by a particular column name on the User > table and I have two aggregates, sum, and count? How do I add these > two columns to q? Do I just call add_column twice? It's not working, > I'm getting an empty result set. > > q = query.session(User) > q = q.add_column(func.sum(User.c.id).label('sum')) > q = q.add_column(func.count(User.c.id).label('count')) > q = q.group_by([User.c.blah]).select() > > I'm getting an empty result set. What exactly does label do here? its going to use the "AS" keyword to apply a column label, such as: select users.id as users_id, users.name as users_name, sum(users.id) as sum, count(users.id) as count FROM users GROUP BY users.blah take a look what its generating and play around with the resulting SQL manually to get an idea about what will return results. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---