Skip to content

Commit cd641d9

Browse files
committedMar 3, 2025
[FIX] records: use temp table in replacing refs
For the jsonb company dependent indirect references when replacing record refs, building the query using the original id mapping input can generate a large query based on the number of references being updated, leading to a 'stack depth limit exceeded' error. We can use the mapping saved in the temp table _upgrade_rrr instead.
1 parent e37d57c commit cd641d9

File tree

1 file changed

+15
-24
lines changed

1 file changed

+15
-24
lines changed
 

‎src/util/records.py

+15-24
Original file line numberDiff line numberDiff line change
@@ -1544,30 +1544,21 @@ def replace_record_references_batch(cr, id_mapping, model_src, model_dst=None, r
15441544
ir.res_id,
15451545
],
15461546
)
1547-
json_path = cr.mogrify(
1548-
"$.* ? ({})".format(" || ".join(["@ == %s"] * len(id_mapping))),
1549-
list(id_mapping),
1550-
).decode()
1551-
1552-
query = cr.mogrify(
1553-
format_query(
1554-
cr,
1555-
"""
1556-
UPDATE {table}
1557-
SET {column} = (
1558-
SELECT jsonb_object_agg(key, COALESCE(((jsonb_object(%s::text[]))->>value)::int, value::int))
1559-
FROM jsonb_each_text({column})
1560-
)
1561-
WHERE {column} IS NOT NULL
1562-
AND {column} @? {json_path}
1563-
""",
1564-
table=ir.table,
1565-
column=ir.res_id,
1566-
json_path=sql.Literal(json_path),
1567-
),
1568-
[list(map(list, id_mapping.items()))],
1569-
).decode()
1570-
explode_execute(cr, query, table=ir.table)
1547+
query = format_query(
1548+
cr,
1549+
"""
1550+
UPDATE {table} t
1551+
SET {column} = (
1552+
SELECT jsonb_object_agg(key, COALESCE((SELECT new FROM _upgrade_rrr WHERE old = value::int), value::int))
1553+
FROM jsonb_each_text(t.{column}))
1554+
FROM _upgrade_rrr map
1555+
WHERE t.{column} IS NOT NULL
1556+
AND (SELECT * FROM JSONB_PATH_QUERY(t.{column}, '$.*'))::int = map.old
1557+
""",
1558+
table=ir.table,
1559+
column=ir.res_id,
1560+
)
1561+
explode_execute(cr, query, table=ir.table, alias="t")
15711562
# ensure all new ids exist
15721563
cr.execute(
15731564
format_query(

0 commit comments

Comments
 (0)