Skip to content
Snippets Groups Projects
Commit 75515d0d authored by Denis Ledoux's avatar Denis Ledoux
Browse files

[FIX] models: prevent fields inherited from `delegate=True` to be deleted

Use case:
 - `./odoo-bin -d mydb -i website`
 - `./odoo-bin -d mydb -i base_automation`
 - `./odoo-bin -d mydb -u website,base_automation`
 ```
 odoo.addons.base.models.ir_model: Deleting 2652@ir.model.fields (base_automation.field_base_automation__website_published)
 odoo.addons.base.models.ir_model: Deleting 2651@ir.model.fields (base_automation.field_base_automation__website_url)
 odoo.addons.base.models.ir_model: Deleting 2650@ir.model.fields (base_automation.field_base_automation__website_path)
 ```

The issue comes from the fact:
- `website` adds multiple website related fields on `ir.actions.server`
  https://github.com/odoo/odoo/blob/30e94d305f9cffa816ddc213e0b9329c0263c145/addons/website/models/ir_actions.py#L16-L18
- `base.automation` inherits by delegation of the `ir.actions.server` fields
  thanks to `delegate=True` on its field `action_server_id`
  https://github.com/odoo/odoo/blob/30e94d305f9cffa816ddc213e0b9329c0263c145/addons/base_automation/models/base_automation.py#L37
- when `base_automation` is installed after `website`
  when `_reflect_model` is called,
  the website related fields on `ir.actions.server` are well in the `_fields` of the `base.automation` model,
  and there an xmlid for these fields is created
  e.g. `field_base_automation__website_published`
  https://github.com/odoo/odoo/blob/30e94d305f9cffa816ddc213e0b9329c0263c145/odoo/addons/base/models/ir_model.py#L881-L882


- during the `-u website,base_automation`, `_reflect_model` on `base.automation` is called before
  the website related fields coming from its inherits on `ir.actions.server` are added in its `_fields`,
  and is not recalled after they are added, when the `website` module is loaded and these website related fields
  are added on `ir.actions.server`.

Because of this, at the end of the upgrade, in the `ir.model.data` `_process_end`,
as the xmlids of these fields have not been loaded,
they are being deleted, because the ORM considers these fields were dropped
from the source code because their xmlids have not been loaded during the upgrade.

Adding the model `base.automation` in the `inherits_children` of `ir.actions.server`
when the delegate field `action_server_id` is added make sure
`_reflect_model` is called on `base.automation`
after the website related field are loaded on the model `ir.actions.server`,
and therefore ensure the xmlids are properly loaded,
therefore preventing the fields deletion.

Additionaly, `delegate` and `inherits` are supposed to be equivalent,
it's just two ways to do the same thing.

Before this revision,
when using `delegate`, `base.automation` is not in the `inherits_children` of `ir.actions.server`:
```
In [1]: env['ir.actions.server']._inherits_children
Out[1]: set()
```

while, by converting the `delegate` to an `inherits`:
```diff
diff --git a/addons/base_automation/models/base_automation.py b/addons/base_automation/models/base_automation.py
index 196ebe9965f..c073150386a 100644
--- a/addons/base_automation/models/base_automation.py
+++ b/addons/base_automation/models/base_automation.py
@@ -30,11 +30,12 @@ class BaseAutomation(models.Model):
     _name = 'base.automation'
     _description = 'Automated Action'
     _order = 'sequence'
+    _inherits = {'ir.actions.server': 'action_server_id'}

     action_server_id = fields.Many2one(
         'ir.actions.server', 'Server Actions',
         domain="[('model_id', '=', model_id)]",
-        delegate=True, required=True, ondelete='restrict')
+        required=True, ondelete='restrict')
     active = fields.Boolean(default=True, help="When unchecked, the rule is hidden and will not be executed.")
     trigger = fields.Selection([
         ('on_create', 'On Creation'),
```
it is:
```
In [1]: env['ir.actions.server']._inherits_children
Out[1]: {'base.automation'}
```

closes odoo/odoo#54119

X-original-commit: 3fd162db
Signed-off-by: default avatarDenis Ledoux (dle) <dle@odoo.com>
parent 4ef5a0d4
No related branches found
No related tags found
No related merge requests found
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment