From 0f43032b82c22a69332b6a01b9d277776a2d644b Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Mon, 18 Feb 2013 15:51:00 +0100 Subject: [PATCH] [FIX] search: when count=True, execute main query as a subquery to avoid side effects with offset and limit bzr revid: rco@openerp.com-20130218145100-4q24j8ko9j9elwpw --- openerp/osv/orm.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/openerp/osv/orm.py b/openerp/osv/orm.py index ebde4c17a51..f43b0d27d3b 100644 --- a/openerp/osv/orm.py +++ b/openerp/osv/orm.py @@ -4870,12 +4870,16 @@ class BaseModel(object): limit_str = limit and ' limit %d' % limit or '' offset_str = offset and ' offset %d' % offset or '' where_str = where_clause and (" WHERE %s" % where_clause) or '' + query_str = 'SELECT "%s".id FROM ' % self._table + from_clause + where_str + order_by + limit_str + offset_str if count: - cr.execute('SELECT count("%s".id) FROM ' % self._table + from_clause + where_str + limit_str + offset_str, where_clause_params) - res = cr.fetchall() - return res[0][0] - cr.execute('SELECT "%s".id FROM ' % self._table + from_clause + where_str + order_by + limit_str + offset_str, where_clause_params) + # /!\ the main query must be executed as a subquery, otherwise + # offset and limit apply to the result of count()! + cr.execute('SELECT count(*) FROM (%s) AS res' % query_str, where_clause_params) + res = cr.fetchone() + return res[0] + + cr.execute(query_str, where_clause_params) res = cr.fetchall() # TDE note: with auto_join, we could have several lines about the same result