[sqlalchemy] Re: aggregation with count and webhelpers.paginate
-Original Message- From: sqlalchemy@googlegroups.com [mailto:sqlalch...@googlegroups.com] On Behalf Of Hollister Sent: 20 June 2009 02:15 To: sqlalchemy Subject: [sqlalchemy] Re: aggregation with count and webhelpers.paginate Well, that worked great: q = meta.Session.query(m.Hit.referer, func.count(m.Hit.id))\ .group_by(m.Hit.referer)\ .order_by(func.count(m.Hit.id).desc()) Thanks! ps: Is there a better way to specify the count in the order_by? If it's just that you don't like repeating yourself, you should be able to save the result of func.count (untested): hit_count = func.count(m.Hit.id) q = (meta.Session.query(m.Hit.referer, hit_count) .group_by(m.Hit.referer) .order_by(hit_count.desc()) Simon --~--~-~--~~~---~--~~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: aggregation with count and webhelpers.paginate
You could also use a label to avoid the repetition: from sqlalchemy.sql import desc meta.Session.query(m.Hit.referer, func.count(m.Hit.id).label('count'))\ .group_by(m.Hit.referer)\ .order_by(desc('count')) On Mon, Jun 22, 2009 at 2:22 AM, King Simon-NFHD78simon.k...@motorola.com wrote: -Original Message- From: sqlalchemy@googlegroups.com [mailto:sqlalch...@googlegroups.com] On Behalf Of Hollister Sent: 20 June 2009 02:15 To: sqlalchemy Subject: [sqlalchemy] Re: aggregation with count and webhelpers.paginate Well, that worked great: q = meta.Session.query(m.Hit.referer, func.count(m.Hit.id))\ .group_by(m.Hit.referer)\ .order_by(func.count(m.Hit.id).desc()) Thanks! ps: Is there a better way to specify the count in the order_by? If it's just that you don't like repeating yourself, you should be able to save the result of func.count (untested): hit_count = func.count(m.Hit.id) q = (meta.Session.query(m.Hit.referer, hit_count) .group_by(m.Hit.referer) .order_by(hit_count.desc()) Simon --~--~-~--~~~---~--~~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: aggregation with count and webhelpers.paginate
Update: If I run: results = meta.Session.execute(s).fetchall() and pass that to paginate, it works. I guess I just needed the ResultProxy, and not a collection of mapped objects. Mike, if you have any additional insight for me, I would appreciate it. On Jun 19, 11:30 am, Hollister a.hollister.willi...@gmail.com wrote: I have a simple query with a group_by and count(), and I'd like to pass this to webhelpers.paginate for display: def referrers(self): s = select([m.hit_table.c.id, m.hit_table.c.referer, func.count (m.Hit.referer).label('count')], from_obj = [m.hit_table], group_by = [m.Hit.referer], order_by = 'count desc') query = meta.Session.query(m.Hit).from_statement(s) c.paginator = paginate.Page( query, page = int(request.params.get('page', 1)), items_per_page = 50, ) return render('/derived/hits/referrer_list.html') The sql generated by this is fine: SELECT hit.id AS hit_id, hit.referer AS hit_referer, count (hit.referer) AS count FROM hit GROUP BY hit.referer ORDER BY count desc and the results are correct. When I run this, I get: Module sqlalchemy.orm.query:1956 in setup_context context.froms.append(self.selectable) if context.order_by is False and self.mapper.order_by: context.order_by = self.mapper.order_by if context.order_by is False and self.mapper.order_by: AttributeError: 'QueryContext' object has no attribute 'order_by' This evidently has nothing to do with the query's order_by clause since I get the same error if I remove it. If I execute the query and pass the results to paginate: results = query.all() c.paginator = paginate.Page( results, page = int(request.params.get('page', 1)), items_per_page = 50, ) I get: AttributeError: 'Hit' object has no attribute 'count' Which I guess makes sense, since I don't have that defined in my class. What am I doing wrong? --~--~-~--~~~---~--~~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: aggregation with count and webhelpers.paginate
The bad news with results = meta.Session.execute(s).fetchall() is that it runs for every page in the paginator, fetching all rows each time. Thoughts, anyone? On Jun 19, 12:17 pm, Hollister a.hollister.willi...@gmail.com wrote: Update: If I run: results = meta.Session.execute(s).fetchall() and pass that to paginate, it works. I guess I just needed the ResultProxy, and not a collection of mapped objects. Mike, if you have any additional insight for me, I would appreciate it. On Jun 19, 11:30 am, Hollister a.hollister.willi...@gmail.com wrote: I have a simple query with a group_by and count(), and I'd like to pass this to webhelpers.paginate for display: def referrers(self): s = select([m.hit_table.c.id, m.hit_table.c.referer, func.count (m.Hit.referer).label('count')], from_obj = [m.hit_table], group_by = [m.Hit.referer], order_by = 'count desc') query = meta.Session.query(m.Hit).from_statement(s) c.paginator = paginate.Page( query, page = int(request.params.get('page', 1)), items_per_page = 50, ) return render('/derived/hits/referrer_list.html') The sql generated by this is fine: SELECT hit.id AS hit_id, hit.referer AS hit_referer, count (hit.referer) AS count FROM hit GROUP BY hit.referer ORDER BY count desc and the results are correct. When I run this, I get: Module sqlalchemy.orm.query:1956 in setup_context context.froms.append(self.selectable) if context.order_by is False and self.mapper.order_by: context.order_by = self.mapper.order_by if context.order_by is False and self.mapper.order_by: AttributeError: 'QueryContext' object has no attribute 'order_by' This evidently has nothing to do with the query's order_by clause since I get the same error if I remove it. If I execute the query and pass the results to paginate: results = query.all() c.paginator = paginate.Page( results, page = int(request.params.get('page', 1)), items_per_page = 50, ) I get: AttributeError: 'Hit' object has no attribute 'count' Which I guess makes sense, since I don't have that defined in my class. What am I doing wrong? --~--~-~--~~~---~--~~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: aggregation with count and webhelpers.paginate
Hollister wrote: When I run this, I get: Module sqlalchemy.orm.query:1956 in setup_context context.froms.append(self.selectable) if context.order_by is False and self.mapper.order_by: context.order_by = self.mapper.order_by if context.order_by is False and self.mapper.order_by: I need a full stack trace on this since once from_statement() is called, setup_context() should never be called. AttributeError: 'QueryContext' object has no attribute 'order_by' This evidently has nothing to do with the query's order_by clause since I get the same error if I remove it. If I execute the query and pass the results to paginate: results = query.all() c.paginator = paginate.Page( results, page = int(request.params.get('page', 1)), items_per_page = 50, ) I get: AttributeError: 'Hit' object has no attribute 'count' Which I guess makes sense, since I don't have that defined in my class. What am I doing wrong? --~--~-~--~~~---~--~~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: aggregation with count and webhelpers.paginate
oh. if the paginator uses slices to provide LIMIT/OFFSET, you need to construct a Query that can be limited via slice. don't use select() here, use Query fully. Hollister wrote: The bad news with results = meta.Session.execute(s).fetchall() is that it runs for every page in the paginator, fetching all rows each time. Thoughts, anyone? On Jun 19, 12:17 pm, Hollister a.hollister.willi...@gmail.com wrote: Update: If I run: results = meta.Session.execute(s).fetchall() and pass that to paginate, it works. I guess I just needed the ResultProxy, and not a collection of mapped objects. Mike, if you have any additional insight for me, I would appreciate it. On Jun 19, 11:30 am, Hollister a.hollister.willi...@gmail.com wrote: I have a simple query with a group_by and count(), and I'd like to pass this to webhelpers.paginate for display: def referrers(self): s = select([m.hit_table.c.id, m.hit_table.c.referer, func.count (m.Hit.referer).label('count')], from_obj = [m.hit_table], group_by = [m.Hit.referer], order_by = 'count desc') query = meta.Session.query(m.Hit).from_statement(s) c.paginator = paginate.Page( query, page = int(request.params.get('page', 1)), items_per_page = 50, ) return render('/derived/hits/referrer_list.html') The sql generated by this is fine: SELECT hit.id AS hit_id, hit.referer AS hit_referer, count (hit.referer) AS count FROM hit GROUP BY hit.referer ORDER BY count desc and the results are correct. When I run this, I get: Module sqlalchemy.orm.query:1956 in setup_context context.froms.append(self.selectable) if context.order_by is False and self.mapper.order_by: context.order_by = self.mapper.order_by if context.order_by is False and self.mapper.order_by: AttributeError: 'QueryContext' object has no attribute 'order_by' This evidently has nothing to do with the query's order_by clause since I get the same error if I remove it. If I execute the query and pass the results to paginate: results = query.all() c.paginator = paginate.Page( results, page = int(request.params.get('page', 1)), items_per_page = 50, ) I get: AttributeError: 'Hit' object has no attribute 'count' Which I guess makes sense, since I don't have that defined in my class. What am I doing wrong? --~--~-~--~~~---~--~~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: aggregation with count and webhelpers.paginate
Here you go: URL: http://dev:5000/hits/referrers File '/home/aw/venv/dev/lib/python2.6/site-packages/WebError-0.10.1- py2.6.egg/weberror/evalexception.py', line 431 in respond app_iter = self.application(environ, detect_start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3- py2.6.egg/beaker/middleware.py', line 81 in __call__ return self.app(environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3- py2.6.egg/beaker/middleware.py', line 160 in __call__ return self.wrap_app(environ, session_start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Routes-1.10.3- py2.6.egg/routes/middleware.py', line 130 in __call__ response = self.app(environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/wsgiapp.py', line 125 in __call__ response = self.dispatch(controller, environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/wsgiapp.py', line 324 in dispatch return controller(environ, start_response) File '/home/aw/projects/resonate/corp/corp/lib/base.py', line 18 in __call__ return WSGIController.__call__(self, environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 221 in __call__ response = self._dispatch_call() File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 172 in _dispatch_call response = self._inspect_call(func) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 107 in _inspect_call result = self._perform_call(func, args) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 60 in _perform_call return func(**args) File '/home/aw/projects/resonate/corp/corp/controllers/hits.py', line 67 in referrers items_per_page = 50, File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4- py2.6.egg/webhelpers/paginate.py', line 333 in __init__ self.item_count = len(self.collection) File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4- py2.6.egg/webhelpers/paginate.py', line 204 in __len__ return self.obj.count() File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1465 in count should_nest = should_nest[0] File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1472 in _col_aggregate entity.setup_context(self, context) File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1956 in setup_context if context.order_by is False and self.mapper.order_by: AttributeError: 'QueryContext' object has no attribute 'order_by' On Jun 19, 12:35 pm, Michael Bayer mike...@zzzcomputing.com wrote: Hollister wrote: When I run this, I get: Module sqlalchemy.orm.query:1956 in setup_context context.froms.append(self.selectable) if context.order_by is False and self.mapper.order_by: context.order_by = self.mapper.order_by if context.order_by is False and self.mapper.order_by: I need a full stack trace on this since once from_statement() is called, setup_context() should never be called. AttributeError: 'QueryContext' object has no attribute 'order_by' This evidently has nothing to do with the query's order_by clause since I get the same error if I remove it. If I execute the query and pass the results to paginate: results = query.all() c.paginator = paginate.Page( results, page = int(request.params.get('page', 1)), items_per_page = 50, ) I get: AttributeError: 'Hit' object has no attribute 'count' Which I guess makes sense, since I don't have that defined in my class. What am I doing wrong? --~--~-~--~~~---~--~~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: aggregation with count and webhelpers.paginate
you can't call count() when you've used from_statement, that should be raising an error. the bug is that no error is being raised. Hollister wrote: Here you go: URL: http://dev:5000/hits/referrers File '/home/aw/venv/dev/lib/python2.6/site-packages/WebError-0.10.1- py2.6.egg/weberror/evalexception.py', line 431 in respond app_iter = self.application(environ, detect_start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3- py2.6.egg/beaker/middleware.py', line 81 in __call__ return self.app(environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3- py2.6.egg/beaker/middleware.py', line 160 in __call__ return self.wrap_app(environ, session_start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Routes-1.10.3- py2.6.egg/routes/middleware.py', line 130 in __call__ response = self.app(environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/wsgiapp.py', line 125 in __call__ response = self.dispatch(controller, environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/wsgiapp.py', line 324 in dispatch return controller(environ, start_response) File '/home/aw/projects/resonate/corp/corp/lib/base.py', line 18 in __call__ return WSGIController.__call__(self, environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 221 in __call__ response = self._dispatch_call() File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 172 in _dispatch_call response = self._inspect_call(func) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 107 in _inspect_call result = self._perform_call(func, args) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 60 in _perform_call return func(**args) File '/home/aw/projects/resonate/corp/corp/controllers/hits.py', line 67 in referrers items_per_page = 50, File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4- py2.6.egg/webhelpers/paginate.py', line 333 in __init__ self.item_count = len(self.collection) File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4- py2.6.egg/webhelpers/paginate.py', line 204 in __len__ return self.obj.count() File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1465 in count should_nest = should_nest[0] File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1472 in _col_aggregate entity.setup_context(self, context) File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1956 in setup_context if context.order_by is False and self.mapper.order_by: AttributeError: 'QueryContext' object has no attribute 'order_by' On Jun 19, 12:35 pm, Michael Bayer mike...@zzzcomputing.com wrote: Hollister wrote: When I run this, I get: Module sqlalchemy.orm.query:1956 in setup_context context.froms.append(self.selectable) if context.order_by is False and self.mapper.order_by: context.order_by = self.mapper.order_by if context.order_by is False and self.mapper.order_by: I need a full stack trace on this since once from_statement() is called, setup_context() should never be called. AttributeError: 'QueryContext' object has no attribute 'order_by' This evidently has nothing to do with the query's order_by clause since I get the same error if I remove it. If I execute the query and pass the results to paginate: results = query.all() c.paginator = paginate.Page( results, page = int(request.params.get('page', 1)), items_per_page = 50, ) I get: AttributeError: 'Hit' object has no attribute 'count' Which I guess makes sense, since I don't have that defined in my class. What am I doing wrong? --~--~-~--~~~---~--~~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: aggregation with count and webhelpers.paginate
Ok, I see. So what's the best way for me to construct and execute this query? On Jun 19, 1:30 pm, Michael Bayer mike...@zzzcomputing.com wrote: you can't call count() when you've used from_statement, that should be raising an error. the bug is that no error is being raised. Hollister wrote: Here you go: URL:http://dev:5000/hits/referrers File '/home/aw/venv/dev/lib/python2.6/site-packages/WebError-0.10.1- py2.6.egg/weberror/evalexception.py', line 431 in respond app_iter = self.application(environ, detect_start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3- py2.6.egg/beaker/middleware.py', line 81 in __call__ return self.app(environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3- py2.6.egg/beaker/middleware.py', line 160 in __call__ return self.wrap_app(environ, session_start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Routes-1.10.3- py2.6.egg/routes/middleware.py', line 130 in __call__ response = self.app(environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/wsgiapp.py', line 125 in __call__ response = self.dispatch(controller, environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/wsgiapp.py', line 324 in dispatch return controller(environ, start_response) File '/home/aw/projects/resonate/corp/corp/lib/base.py', line 18 in __call__ return WSGIController.__call__(self, environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 221 in __call__ response = self._dispatch_call() File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 172 in _dispatch_call response = self._inspect_call(func) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 107 in _inspect_call result = self._perform_call(func, args) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 60 in _perform_call return func(**args) File '/home/aw/projects/resonate/corp/corp/controllers/hits.py', line 67 in referrers items_per_page = 50, File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4- py2.6.egg/webhelpers/paginate.py', line 333 in __init__ self.item_count = len(self.collection) File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4- py2.6.egg/webhelpers/paginate.py', line 204 in __len__ return self.obj.count() File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1465 in count should_nest = should_nest[0] File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1472 in _col_aggregate entity.setup_context(self, context) File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1956 in setup_context if context.order_by is False and self.mapper.order_by: AttributeError: 'QueryContext' object has no attribute 'order_by' On Jun 19, 12:35 pm, Michael Bayer mike...@zzzcomputing.com wrote: Hollister wrote: When I run this, I get: Module sqlalchemy.orm.query:1956 in setup_context context.froms.append(self.selectable) if context.order_by is False and self.mapper.order_by: context.order_by = self.mapper.order_by if context.order_by is False and self.mapper.order_by: I need a full stack trace on this since once from_statement() is called, setup_context() should never be called. AttributeError: 'QueryContext' object has no attribute 'order_by' This evidently has nothing to do with the query's order_by clause since I get the same error if I remove it. If I execute the query and pass the results to paginate: results = query.all() c.paginator = paginate.Page( results, page = int(request.params.get('page', 1)), items_per_page = 50, ) I get: AttributeError: 'Hit' object has no attribute 'count' Which I guess makes sense, since I don't have that defined in my class. What am I doing wrong? --~--~-~--~~~---~--~~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: aggregation with count and webhelpers.paginate
Hollister wrote: Ok, I see. So what's the best way for me to construct and execute this query? use Query: session.query(MyClass.someid, MyClass.somethingelse).filter(..whatever..).order_by(..whatever...) On Jun 19, 1:30 pm, Michael Bayer mike...@zzzcomputing.com wrote: you can't call count() when you've used from_statement, that should be raising an error. the bug is that no error is being raised. Hollister wrote: Here you go: URL:http://dev:5000/hits/referrers File '/home/aw/venv/dev/lib/python2.6/site-packages/WebError-0.10.1- py2.6.egg/weberror/evalexception.py', line 431 in respond app_iter = self.application(environ, detect_start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3- py2.6.egg/beaker/middleware.py', line 81 in __call__ return self.app(environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3- py2.6.egg/beaker/middleware.py', line 160 in __call__ return self.wrap_app(environ, session_start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Routes-1.10.3- py2.6.egg/routes/middleware.py', line 130 in __call__ response = self.app(environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/wsgiapp.py', line 125 in __call__ response = self.dispatch(controller, environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/wsgiapp.py', line 324 in dispatch return controller(environ, start_response) File '/home/aw/projects/resonate/corp/corp/lib/base.py', line 18 in __call__ return WSGIController.__call__(self, environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 221 in __call__ response = self._dispatch_call() File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 172 in _dispatch_call response = self._inspect_call(func) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 107 in _inspect_call result = self._perform_call(func, args) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 60 in _perform_call return func(**args) File '/home/aw/projects/resonate/corp/corp/controllers/hits.py', line 67 in referrers items_per_page = 50, File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4- py2.6.egg/webhelpers/paginate.py', line 333 in __init__ self.item_count = len(self.collection) File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4- py2.6.egg/webhelpers/paginate.py', line 204 in __len__ return self.obj.count() File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1465 in count should_nest = should_nest[0] File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1472 in _col_aggregate entity.setup_context(self, context) File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1956 in setup_context if context.order_by is False and self.mapper.order_by: AttributeError: 'QueryContext' object has no attribute 'order_by' On Jun 19, 12:35 pm, Michael Bayer mike...@zzzcomputing.com wrote: Hollister wrote: When I run this, I get: Module sqlalchemy.orm.query:1956 in setup_context context.froms.append(self.selectable) if context.order_by is False and self.mapper.order_by: context.order_by = self.mapper.order_by if context.order_by is False and self.mapper.order_by: I need a full stack trace on this since once from_statement() is called, setup_context() should never be called. AttributeError: 'QueryContext' object has no attribute 'order_by' This evidently has nothing to do with the query's order_by clause since I get the same error if I remove it. If I execute the query and pass the results to paginate: results = query.all() c.paginator = paginate.Page( results, page = int(request.params.get('page', 1)), items_per_page = 50, ) I get: AttributeError: 'Hit' object has no attribute 'count' Which I guess makes sense, since I don't have that defined in my class. What am I doing wrong? --~--~-~--~~~---~--~~ 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 sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en
[sqlalchemy] Re: aggregation with count and webhelpers.paginate
Well, that worked great: q = meta.Session.query(m.Hit.referer, func.count(m.Hit.id))\ .group_by(m.Hit.referer)\ .order_by(func.count(m.Hit.id).desc()) Thanks! ps: Is there a better way to specify the count in the order_by? On Jun 19, 2:58 pm, Michael Bayer mike...@zzzcomputing.com wrote: Hollister wrote: Ok, I see. So what's the best way for me to construct and execute this query? use Query: session.query(MyClass.someid, MyClass.somethingelse).filter(..whatever..).order_by(..whatever...) On Jun 19, 1:30 pm, Michael Bayer mike...@zzzcomputing.com wrote: you can't call count() when you've used from_statement, that should be raising an error. the bug is that no error is being raised. Hollister wrote: Here you go: URL:http://dev:5000/hits/referrers File '/home/aw/venv/dev/lib/python2.6/site-packages/WebError-0.10.1- py2.6.egg/weberror/evalexception.py', line 431 in respond app_iter = self.application(environ, detect_start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3- py2.6.egg/beaker/middleware.py', line 81 in __call__ return self.app(environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Beaker-1.3- py2.6.egg/beaker/middleware.py', line 160 in __call__ return self.wrap_app(environ, session_start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Routes-1.10.3- py2.6.egg/routes/middleware.py', line 130 in __call__ response = self.app(environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/wsgiapp.py', line 125 in __call__ response = self.dispatch(controller, environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/wsgiapp.py', line 324 in dispatch return controller(environ, start_response) File '/home/aw/projects/resonate/corp/corp/lib/base.py', line 18 in __call__ return WSGIController.__call__(self, environ, start_response) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 221 in __call__ response = self._dispatch_call() File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 172 in _dispatch_call response = self._inspect_call(func) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 107 in _inspect_call result = self._perform_call(func, args) File '/home/aw/venv/dev/lib/python2.6/site-packages/Pylons-0.9.7- py2.6.egg/pylons/controllers/core.py', line 60 in _perform_call return func(**args) File '/home/aw/projects/resonate/corp/corp/controllers/hits.py', line 67 in referrers items_per_page = 50, File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4- py2.6.egg/webhelpers/paginate.py', line 333 in __init__ self.item_count = len(self.collection) File '/home/aw/venv/dev/lib/python2.6/site-packages/WebHelpers-0.6.4- py2.6.egg/webhelpers/paginate.py', line 204 in __len__ return self.obj.count() File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1465 in count should_nest = should_nest[0] File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1472 in _col_aggregate entity.setup_context(self, context) File '/home/aw/venv/dev/lib/python2.6/site-packages/SQLAlchemy-0.5.2- py2.6.egg/sqlalchemy/orm/query.py', line 1956 in setup_context if context.order_by is False and self.mapper.order_by: AttributeError: 'QueryContext' object has no attribute 'order_by' On Jun 19, 12:35 pm, Michael Bayer mike...@zzzcomputing.com wrote: Hollister wrote: When I run this, I get: Module sqlalchemy.orm.query:1956 in setup_context context.froms.append(self.selectable) if context.order_by is False and self.mapper.order_by: context.order_by = self.mapper.order_by if context.order_by is False and self.mapper.order_by: I need a full stack trace on this since once from_statement() is called, setup_context() should never be called. AttributeError: 'QueryContext' object has no attribute 'order_by' This evidently has nothing to do with the query's order_by clause since I get the same error if I remove it. If I execute the query and pass the results to paginate: results = query.all() c.paginator = paginate.Page( results, page = int(request.params.get('page', 1)), items_per_page = 50, ) I get: AttributeError: 'Hit' object has no attribute 'count' Which I guess makes sense, since I don't have that defined in my class. What am I doing wrong?