Skip to content

Commit 50bc045

Browse files
committed
[FIX] records: allow change of comodel for comp dep fields
When replacing refereces by batch, the assertion that the model source == model dest is unique for company dependent fields, other indirect references get their model updated in this case. So we can remove the assertion and change the comodel for jsonb company dependent fields too. closes #160 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent b5e03e1 commit 50bc045

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/util/records.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,15 @@ def replace_record_references_batch(cr, id_mapping, model_src, model_dst=None, r
14791479
continue
14801480
if ir.company_dependent_comodel:
14811481
if ir.company_dependent_comodel == model_src:
1482-
assert model_src == model_dst
1482+
if model_src != model_dst:
1483+
cr.execute(
1484+
"UPDATE ir_model_fields SET relation = %s WHERE model = %s AND name = %s",
1485+
[
1486+
model_dst,
1487+
model_of_table(cr, ir.table),
1488+
ir.res_id,
1489+
],
1490+
)
14831491
json_path = cr.mogrify(
14841492
"$.* ? ({})".format(" || ".join(["@ == %s"] * len(id_mapping))),
14851493
list(id_mapping),
@@ -1504,6 +1512,33 @@ def replace_record_references_batch(cr, id_mapping, model_src, model_dst=None, r
15041512
[list(map(list, id_mapping.items()))],
15051513
).decode()
15061514
explode_execute(cr, query, table=ir.table)
1515+
# ensure all new ids exist
1516+
cr.execute(
1517+
format_query(
1518+
cr,
1519+
"""
1520+
Select t.id AS id,
1521+
j.key AS c_id,
1522+
j.value AS ref
1523+
FROM {table} t
1524+
JOIN JSONB_EACH(t.{column}) j
1525+
ON True
1526+
WHERE NOT EXISTS (
1527+
SELECT 1 FROM {dest_table} WHERE id = j.value::int
1528+
)
1529+
""",
1530+
table=ir.table,
1531+
column=ir.res_id,
1532+
dest_table=table_of_model(cr, model_dst),
1533+
)
1534+
)
1535+
invalid_ref = cr.dictfetchall()
1536+
if invalid_ref:
1537+
raise RuntimeError(
1538+
"Invalid company dependent values for {}.{} referencing model {}: {}".format(
1539+
model_of_table(cr, ir.table), ir.res_id, model_dst, invalid_ref
1540+
)
1541+
)
15071542
continue
15081543
res_model_upd = []
15091544
if ir.res_model:

0 commit comments

Comments
 (0)