diff options
| author | Raphael Collet <rco@odoo.com> | 2020-06-19 12:00:09 +0000 |
|---|---|---|
| committer | Raphael Collet <rco@odoo.com> | 2020-06-23 14:00:42 +0000 |
| commit | ea83473eab2ba3f23673dd2561f3c80550a4671d (patch) | |
| tree | 0fbca11adeb3cb3d363fc20bb595ac137ed53e22 | |
| parent | 536759644e13448f1c2b14d95cc8ecb44e5f0b24 (diff) | |
[FIX] core: flush() with one2many fields
This ensures that flushing a one2many field automatically flushes its
inverse many2one/integer field. Without that, a search like:
model.search([('o2m_ids', 'in', ids)])
can return incorrect results.
closes odoo/odoo#53514
X-original-commit: b91b485a5d9a62acfe44b12c64a46dcca23981ce
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
| -rw-r--r-- | odoo/addons/test_new_api/tests/test_new_fields.py | 15 | ||||
| -rw-r--r-- | odoo/models.py | 6 |
2 files changed, 21 insertions, 0 deletions
diff --git a/odoo/addons/test_new_api/tests/test_new_fields.py b/odoo/addons/test_new_api/tests/test_new_fields.py index fa2f8007bb4..ef3ce6b1c0a 100644 --- a/odoo/addons/test_new_api/tests/test_new_fields.py +++ b/odoo/addons/test_new_api/tests/test_new_fields.py @@ -1625,6 +1625,21 @@ class TestFields(TransactionCaseWithUserDemo): line.move_id = move2 self.assertEqual(line.move_id, move2) + def test_72_relational_inverse(self): + """ Check the consistency of relational fields with inverse(s). """ + move1 = self.env['test_new_api.move'].create({}) + move2 = self.env['test_new_api.move'].create({}) + + # makes sure that line.move_id is flushed before search + line = self.env['test_new_api.move_line'].create({'move_id': move1.id}) + moves = self.env['test_new_api.move'].search([('line_ids', 'in', line.id)]) + self.assertEqual(moves, move1) + + # makes sure that line.move_id is flushed before search + line.move_id = move2 + moves = self.env['test_new_api.move'].search([('line_ids', 'in', line.id)]) + self.assertEqual(moves, move2) + def test_80_copy(self): Translations = self.env['ir.translation'] discussion = self.env.ref('test_new_api.discussion_0') diff --git a/odoo/models.py b/odoo/models.py index 42b1344e2e4..f4b0012dfff 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -5349,6 +5349,12 @@ Record ids: %(records)s id_vals = self.env.all.towrite.pop(model_name) process(self.env[model_name], id_vals) + # missing for one2many fields, flush their inverse + for fname in fnames: + field = self._fields[fname] + if field.type == 'one2many' and field.inverse_name: + self.env[field.comodel_name].flush([field.inverse_name]) + # # New records - represent records that do not exist in the database yet; # they are used to perform onchanges. |
