From b70f159f3a24dbc8a83f6d577bc36d52b75385d8 Mon Sep 17 00:00:00 2001 From: Damien Bouvy Date: Mon, 22 Aug 2016 17:26:16 +0200 Subject: [PATCH] [FIX] product: avoid duplicates in name search A typo probably introduced during the migration to the new api may have caused duplicates to appear in the name search results since it added the ids of the wrong recordset. The previous fix, while effective, also removed a previous optimization for databases with a lot of products. While the code may seem obscure on first read, the comments should shed some light on the implementation choice. This commit therefore reintroduce the previous code with the exception of the call to the wrong recordset (self instead of products). Amends b02a85f Courtesy of @rco-odoo --- addons/product/models/product.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/addons/product/models/product.py b/addons/product/models/product.py index 0d9207a44d9..7cc87f3eb56 100644 --- a/addons/product/models/product.py +++ b/addons/product/models/product.py @@ -425,7 +425,11 @@ class ProductProduct(models.Model): # on a database with thousands of matching products, due to the huge merge+unique needed for the # OR operator (and given the fact that the 'name' lookup results come from the ir.translation table # Performing a quick memory merge of ids in Python will give much better performance - products = self.search(args + ['|', ('default_code', operator, name), ('name', operator, name)], limit=limit) + products = self.search(args + [('default_code', operator, name)], limit=limit) + if not limit or len(products) < limit: + # we may underrun the limit because of dupes in the results, that's fine + limit2 = (limit - len(products)) if limit else False + products += self.search(args + [('name', operator, name), ('id', 'not in', products.ids)], limit=limit2) elif not products and operator in expression.NEGATIVE_TERM_OPERATORS: products = self.search(args + ['&', ('default_code', operator, name), ('name', operator, name)], limit=limit) if not products and operator in positive_operators: