From cc64d52b33cdaba5a2e3563525bc6b189251eea2 Mon Sep 17 00:00:00 2001 From: "P. Christeas" Date: Thu, 23 Dec 2010 13:29:38 +0200 Subject: [PATCH] expression: fix m2o handling of empty lists [Bug 598454] The domain expressions wouldn't yield the expected results in the following cases (eg. for res.partner): [('company_id', 'in', [])] : should return [], not all partners [('company_id.partner_id', 'in', [])] : again, empty set After some deliberation and discussion, we *change the behavior* for them. Notes: 1. This comes after commit 913e95856f80, bug 626806 which solved the corresponding cases for m2m, o2m 2. This commit _will_ have side-effects. The result set for domains now is indeed different. But, we believe, now it is the proper one, fixes cases of unexpected results that have been reported 3. It makes the server a bit faster, because empty results are quick to compute 4. The "not in []" operator will NOT return results with NULL values in the corresponding field (aka. empty). This may not be the one expected by users, but it follows the SQL standard (par. 9.20.3): http://www.postgresql.org/docs/9.0/static/functions-subquery.html bzr revid: p_christ@hol.gr-20101223112938-ore0dmmasekdcyfn --- bin/osv/expression.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/bin/osv/expression.py b/bin/osv/expression.py index 3281fd29f7c..cbb6a0f985b 100644 --- a/bin/osv/expression.py +++ b/bin/osv/expression.py @@ -143,12 +143,18 @@ class expression(object): if len(fargs) > 1: if field._type == 'many2one': right = field_obj.search(cr, uid, [(fargs[1], operator, right)], context=context) - self.__exp[i] = (fargs[0], 'in', right) + if right == []: + self.__exp[i] = ( 'id', '=', 0 ) + else: + self.__exp[i] = (fargs[0], 'in', right) # Making search easier when there is a left operand as field.o2m or field.m2m if field._type in ['many2many','one2many']: right = field_obj.search(cr, uid, [(fargs[1], operator, right)], context=context) right1 = table.search(cr, uid, [(fargs[0],'in', right)], context=context) - self.__exp[i] = ('id', 'in', right1) + if right1 == []: + self.__exp[i] = ( 'id', '=', 0 ) + else: + self.__exp[i] = ('id', 'in', right1) if not isinstance(field,fields.property): continue @@ -313,6 +319,13 @@ class expression(object): if not isinstance(ele, basestring): m2o_str = False break + elif right == []: + m2o_str = False + if operator in ('not in', '!=', '<>'): + # (many2one not in []) should return all records + self.__exp[i] = self.__DUMMY_LEAF + else: + self.__exp[i] = ('id','=',0) else: new_op = '=' if operator in ['not like','not ilike','not in','<>','!=']: