Skip to content

Commit eef47dd

Browse files
committed
[FIX] pg: rename m2m field meta data
When renaming a model and updating related m2m fields, the relation table and its columns are renamed but only manual fields meta data is updated, assuming that base fields will be updated when the module loads. The custom modules meta data should also be updated if the tables were updated.
1 parent 25cb373 commit eef47dd

File tree

1 file changed

+35
-15
lines changed

1 file changed

+35
-15
lines changed

src/util/pg.py

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@
3636
from psycopg2.extras import Json
3737

3838
try:
39-
from odoo.modules import module as odoo_module
39+
from odoo.modules import get_modules, module as odoo_module
4040
from odoo.sql_db import db_connect
4141
except ImportError:
42+
from openerp.modules import get_modules
4243
from openerp.sql_db import db_connect
4344

4445
odoo_module = None
@@ -1462,6 +1463,7 @@ def update_m2m_tables(cr, old_table, new_table, ignored_m2ms=()):
14621463
if old_table == new_table or not version_gte("10.0"):
14631464
return
14641465
ignored_m2ms = set(ignored_m2ms)
1466+
standard_modules = set(get_modules()) - {"studio_customization", "__cloc_exclude__"}
14651467
for orig_m2m_table in get_m2m_tables(cr, new_table):
14661468
if orig_m2m_table in ignored_m2ms:
14671469
continue
@@ -1474,12 +1476,18 @@ def update_m2m_tables(cr, old_table, new_table, ignored_m2ms=()):
14741476
rename_table(cr, orig_m2m_table, m2m_table, remove_constraints=False)
14751477
cr.execute(
14761478
"""
1477-
UPDATE ir_model_fields
1479+
UPDATE ir_model_fields f
14781480
SET relation_table = %s
1479-
WHERE relation_table = %s
1480-
AND state = 'manual'
1481+
FROM ir_model_data d
1482+
WHERE f.relation_table = %s
1483+
AND (
1484+
f.state = 'manual'
1485+
OR d.model = 'ir.model.fields'
1486+
AND d.res_id = f.id
1487+
AND d.module NOT IN %s
1488+
)
14811489
""",
1482-
[m2m_table, orig_m2m_table],
1490+
[m2m_table, orig_m2m_table, tuple(standard_modules)],
14831491
)
14841492
_logger.info("Renamed m2m table %s to %s", orig_m2m_table, m2m_table)
14851493
else:
@@ -1531,23 +1539,35 @@ def update_m2m_tables(cr, old_table, new_table, ignored_m2ms=()):
15311539

15321540
cr.execute(
15331541
"""
1534-
UPDATE ir_model_fields
1542+
UPDATE ir_model_fields f
15351543
SET column1 = %s
1536-
WHERE relation_table = %s
1537-
AND column1 = %s
1538-
AND state = 'manual'
1544+
FROM ir_model_data d
1545+
WHERE f.relation_table = %s
1546+
AND f.column1 = %s
1547+
AND (
1548+
f.state = 'manual'
1549+
OR d.model = 'ir.model.fields'
1550+
AND d.res_id = f.id
1551+
AND d.module NOT IN %s
1552+
)
15391553
""",
1540-
[new_col, m2m_table, old_col],
1554+
[new_col, m2m_table, old_col, tuple(standard_modules)],
15411555
)
15421556
cr.execute(
15431557
"""
1544-
UPDATE ir_model_fields
1558+
UPDATE ir_model_fields f
15451559
SET column2 = %s
1546-
WHERE relation_table = %s
1547-
AND column2 = %s
1548-
AND state = 'manual'
1560+
FROM ir_model_data d
1561+
WHERE f.relation_table = %s
1562+
AND f.column2 = %s
1563+
AND (
1564+
f.state = 'manual'
1565+
OR d.model = 'ir.model.fields'
1566+
AND d.res_id = f.id
1567+
AND d.module NOT IN %s
1568+
)
15491569
""",
1550-
[new_col, m2m_table, old_col],
1570+
[new_col, m2m_table, old_col, tuple(standard_modules)],
15511571
)
15521572

15531573
_logger.info("Renamed m2m column of table %s from %s to %s", m2m_table, old_col, new_col)

0 commit comments

Comments
 (0)