Thursday, February 2, 2023
2 changes · master
Resolved issues and error corrections
This fixes an issue where updating a many-to-many relationship could cause far more related records to be recalculated than necessary. The change reduces unnecessary background work, improving performance for databases with large linked record sets while keeping computed values accurate.
Original PR description
The issue occurs when a computed field depends on a many2many field with a corresponding inverse field on its comodel. Consider two models like ```py class User(models.Model): _name = _description =…
The issue occurs when a computed field depends on a many2many field with a corresponding inverse field on its comodel. Consider two models like
```py
class User(models.Model):
_name = _description = 'test_new_api.user'
group_ids = fields.Many2many('test_new_api.group')
group_count = fields.Integer(compute='_compute_group_count', store=True)
@api.depends('group_ids')
def _compute_group_count(self):
for user in self:
user.group_count = len(user.group_ids)
class Group(models.Model):
_name = _description = 'test_new_api.group'
user_ids = fields.Many2many('test_new_api.user')
```
When a user is added to a group with
```py
group.write({'user_ids': [Command.link(user.id)]})
```
we expect the field `group_count` to be recomputed on `user` only, but it is actually triggered on *all* the records in `group.user_ids`. This is a real performance issue when there are many records in the relation.
The explanation comes from the fact that
* the framework considers the field `user_ids` is modified on `group`;
* the field `group_count` implicitly depends on `group_ids.user_ids`, which makes it triggered on the users `u` such that `u.group_ids` intersects `group`.
The solution consists in handling the dependencies on inverse many2many field in the field itself. The field no longer adds the implicit dependency on its inverse field in the trigger tree, but instead determines which records in the comodel are actually impacted by the relation change in the method field.write().This update fixes how subscription-related operations are handled in batches, helping reduce errors when multiple records are processed together. It should make subscription management more reliable for teams working with larger volumes of sales subscriptions.
Original PR description
The goal of this PR is to batch operation for sale_subscription