summaryrefslogtreecommitdiff
path: root/odoo/addons
diff options
context:
space:
mode:
authorIvan Yelizariev <iel@odoo.com>2021-01-28 16:14:41 +0000
committerRaphael Collet <rco@odoo.com>2021-02-02 15:18:41 +0000
commitd66652b4aa514546356306e3eb48eaecaba07c33 (patch)
tree0bc86672ef4a828be3e7ca7989ed4c537aef5e79 /odoo/addons
parent7840b21483c0012174e6028a39b114ce56f06158 (diff)
[FIX] core: initialize new stored related field via SQL
Filling the new column in SQL is dramatically faster than doing it via the ORM, which iterates over all the records in the table. The test case is creating a related field for 100+ records with distinct values. Setting the field's value was taking more than 100 queries. It now takes 3 queries. task-2449313 opw-2380445 opw-2389376 https://github.com/odoo/enterprise/pull/15910 closes odoo/odoo#65421 X-original-commit: 82644c3ea20f7b5748f92ce4d0f4585335995d29 Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
Diffstat (limited to 'odoo/addons')
-rw-r--r--odoo/addons/base/tests/test_ir_actions.py45
1 files changed, 45 insertions, 0 deletions
diff --git a/odoo/addons/base/tests/test_ir_actions.py b/odoo/addons/base/tests/test_ir_actions.py
index 629c721aefc..76a6f6e2f7a 100644
--- a/odoo/addons/base/tests/test_ir_actions.py
+++ b/odoo/addons/base/tests/test_ir_actions.py
@@ -480,6 +480,51 @@ class TestCustomFields(common.TransactionCase):
self.assertTrue(custom_binary.attachment)
+ def test_related_field(self):
+ """ create a custom related field, and check filled values """
+ #
+ # Add a custom field equivalent to the following definition:
+ #
+ # class Partner(models.Model)
+ # _inherit = 'res.partner'
+ # x_oh_boy = fields.Char(related="country_id.code", store=True)
+ #
+
+ # pick N=100 records in comodel
+ countries = self.env['res.country'].search([('code', '!=', False)], limit=100)
+ self.assertEqual(len(countries), 100, "Not enough records in comodel 'res.country'")
+
+ # create records in model, with N distinct values for the related field
+ partners = self.env['res.partner'].create([
+ {'name': country.code, 'country_id': country.id} for country in countries
+ ])
+ partners.flush()
+
+ # determine how many queries it takes to create a non-computed field
+ query_count = self.cr.sql_log_count
+ self.env['ir.model.fields'].create({
+ 'model_id': self.env['ir.model']._get_id('res.partner'),
+ 'name': 'x_oh_box',
+ 'field_description': 'x_oh_box',
+ 'ttype': 'char',
+ })
+ query_count = self.cr.sql_log_count - query_count
+
+ # create the related field, and assert it only takes 3 extra queries
+ with self.assertQueryCount(query_count + 3):
+ self.env['ir.model.fields'].create({
+ 'model_id': self.env['ir.model']._get_id('res.partner'),
+ 'name': 'x_oh_boy',
+ 'field_description': 'x_oh_boy',
+ 'ttype': 'char',
+ 'related': 'country_id.code',
+ 'store': True,
+ })
+
+ # check the computed values
+ for partner in partners:
+ self.assertEqual(partner.x_oh_boy, partner.country_id.code)
+
def test_selection(self):
""" custom selection field """
Model = self.env[self.MODEL]