From 13bddabbe046588d52f3701e7541f22eaec43088 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Voet=20=28ryv=29?= Date: Wed, 2 Mar 2022 15:07:08 +0000 Subject: [PATCH] [FIX] product: fix performance bottleneck of populate Since 4911a26d2c242a999e1901ffc007a50449d03eac, `_check_barcode_uniqueness` flush the default `_order` of `product.product` models (via many2one in the `_order` of `product.packaging`). It creates a bottleneck (take > 10 minutes ) when we populate (medium size) the `product.template` during `set_barcode_variant`. Force the order to be trivial and simplify the domain with a 'in' instead of multiple OR. closes odoo/odoo#85687 Signed-off-by: William Henrotin (whe) --- addons/product/models/product_packaging.py | 6 +++--- addons/product/models/product_product.py | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/addons/product/models/product_packaging.py b/addons/product/models/product_packaging.py index 9e622054c19..7f00d87a030 100644 --- a/addons/product/models/product_packaging.py +++ b/addons/product/models/product_packaging.py @@ -30,10 +30,10 @@ class ProductPackaging(models.Model): @api.constrains('barcode') def _check_barcode_uniqueness(self): - """ With GS1 nomenclature, products and packagins use the same pattern. Therefore, we need + """ With GS1 nomenclature, products and packagings use the same pattern. Therefore, we need to ensure the uniqueness between products' barcodes and packagings' ones""" - condition = expression.OR([[('barcode', '=', b)] for b in self.mapped('barcode') if b]) - if self.env['product.product'].search(condition, limit=1): + domain = [('barcode', 'in', [b for b in self.mapped('barcode') if b])] + if self.env['product.product'].search(domain, order="id", limit=1): raise ValidationError(_("A product already uses the barcode")) def _check_qty(self, product_qty, uom_id, rounding_method="HALF-UP"): diff --git a/addons/product/models/product_product.py b/addons/product/models/product_product.py index bf8d027ce60..ed567100856 100644 --- a/addons/product/models/product_product.py +++ b/addons/product/models/product_product.py @@ -174,10 +174,10 @@ class ProductProduct(models.Model): @api.constrains('barcode') def _check_barcode_uniqueness(self): - """ With GS1 nomenclature, products and packagins use the same pattern. Therefore, we need + """ With GS1 nomenclature, products and packagings use the same pattern. Therefore, we need to ensure the uniqueness between products' barcodes and packagings' ones""" - condition = expression.OR([[('barcode', '=', b)] for b in self.mapped('barcode') if b]) - if self.env['product.packaging'].search(condition, limit=1): + domain = [('barcode', 'in', [b for b in self.mapped('barcode') if b])] + if self.env['product.packaging'].search(domain, order="id", limit=1): raise ValidationError(_("A packaging already uses the barcode")) def _get_invoice_policy(self):