Skip to content

Commit 133d859

Browse files
committed
[IMP] util/records: skip replace if all ids are the same
If all ids are the same and we are replacing in the same model we currently cause an error. This is not necessary and could lead to false errors if the list of mapping ids is correctly the same. For example sometimes we transform a model A into model B by updating metadata. Then we insert into B's table, and finally perform a replace of references from A's table to B's table. This would fail if the ids are the same. This case will happen with a high probability if there is only one A record with id=1 since the single B record would get id=1 as well. Example logs after the patch: ``` 2025-04-14 07:28:32,883 42811 WARNING test_16 odoo.upgrade.util.records: Replace references in model `res.country`, ignoring same-id mapping `{1: 1, 2: 2}` 2025-04-14 07:28:32,883 42811 WARNING test_16 odoo.upgrade.util.records: Nothing to replace in model `res.country`, ignoring empty mapping ```
1 parent f0dc8c5 commit 133d859

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/base/tests/test_util.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,11 @@ def test_replace_record_references_batch__uniqueness(self):
14491449
[count] = self.env.cr.fetchone()
14501450
self.assertEqual(count, 1)
14511451

1452+
@mute_logger("odoo.upgrade.util.records")
1453+
def test_replace_record_references_batch__idem_mapping(self):
1454+
# This logs a warning but shouldn't fail
1455+
util.replace_record_references_batch(self.env.cr, {1: 1, 2: 2}, "res.country")
1456+
14521457
def _prepare_test_delete_unused(self):
14531458
def create_cat():
14541459
name = f"test_{uuid.uuid4().hex}"

src/util/records.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,9 @@ def replace_record_references_batch(cr, id_mapping, model_src, model_dst=None, r
14691469
if same_ids:
14701470
_logger.warning("Replace references in model `%s`, ignoring same-id mapping `%s`", model_src, same_ids)
14711471
id_mapping = {k: v for k, v in id_mapping.items() if k != v}
1472+
if not id_mapping:
1473+
_logger.warning("Nothing to replace in model `%s`, ignoring empty mapping", model_src)
1474+
return
14721475

14731476
assert id_mapping
14741477
assert all(isinstance(v, int) and isinstance(k, int) for k, v in id_mapping.items())

0 commit comments

Comments
 (0)