Skip to content

Commit 173f53e

Browse files
committed
[FIX] util/models: Prevent removal manual relation table
There are two model ``A`` and ``B`` and there is ``x`` many2many field in ``A`` model which is manual and relation with ``B``. Now, ``B`` model is removed. So, For that relation table is droped but the field will there and in relation model ``_unknown`` will be set https://github.com/odoo/upgrade-util/blob/25cb37389b527f442acfbb704e07769d69b11b9f/src/util/models.py#L242. As the field belongs to ``A``. So, table will be recreated from here as it checks only table is there or not https://github.com/odoo/odoo/blob/54f10b56f577ad9ed5575bd396dba7d20d22fc2e/odoo/fields.py#L4977 and after that from here env variable it have in gone and below menitoned traceback will raise. To Fix removing M2m field and added into migration report ``` Traceback (most recent call last): File "/home/odoo/src/odoo/19.0/odoo/service/server.py", line 1509, in preload_registries registry = Registry.new(dbname, update_module=update_module, install_modules=config['init'], upgrade_modules=config['update'], reinit_modules=config['reinit']) File "/home/odoo/src/odoo/19.0/odoo/tools/func.py", line 88, in locked return func(inst, *args, **kwargs) File "/home/odoo/src/odoo/19.0/odoo/orm/registry.py", line 185, in new load_modules( File "/home/odoo/src/odoo/19.0/odoo/modules/loading.py", line 580, in load_modules model._register_hook() File "/tmp/tmpkw62chtg/migrations/base/0.0.0/pre-models-ir_model_relation.py", line 49, in _register_hook raise util.MigrationError("The following m2m relations have respawn:\n%s" % back_m2m) odoo.upgrade.util.exceptions.UpgradeError: The following m2m relations have respawn: - x_res_partner_uom_category_rel via uom.category ``` upg-3191893 opw-5175564
1 parent 25cb373 commit 173f53e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/util/models.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,35 @@ def remove_model(cr, model, drop_table=True, ignore_m2m=()):
193193
if ignore_m2m != "*":
194194
tables = get_m2m_tables(cr, table_of_model(cr, model))
195195
ignore = set(ignore_m2m)
196+
# manual fields M2M handle should be explicitly
197+
if tables:
198+
cr.execute(
199+
"""
200+
SELECT name, model
201+
FROM ir_model_fields
202+
WHERE relation_table IN %s
203+
AND state = 'manual'
204+
""",
205+
[tuple(tables)],
206+
)
207+
208+
if cr.rowcount:
209+
affected_fields = cr.fetchall()
210+
for field, field_model in affected_fields:
211+
remove_field(cr, field_model, field, drop_column=False)
212+
add_to_migration_reports(
213+
message="""
214+
The following Many2Many fields have been deprecated because their related %s (%s)
215+
model is deprecated: \n%s
216+
"""
217+
% (
218+
model,
219+
mod_label,
220+
"\n".join("- {}".format(field) for field, _ in affected_fields),
221+
),
222+
category="Removed Fields",
223+
)
224+
196225
for table_name in tables:
197226
if table_name in ignore:
198227
_logger.info("remove_model(%r): m2m table %r explicitly ignored", model, table_name)

0 commit comments

Comments
 (0)