1
votes

I have observed the table where an One2many field is related. When I deleted a row of an One2many field thorough One2many widget the real row inside the table is not deleted.

The row just got the foreign key column set to NULL.

Can we change this behavior to permanently (hard) delete the related row?

Why did the designer choose this behavior in the first place? Any technical consideration why?

Thanks

1

1 Answers

2
votes

In relation field, there is parameter Ondelete. It has 3 options

  1. Cascade (delete when relation field is delete.)
  2. Set to NULL (set null value in relation field.)
  3. Restrict (Will not allow you to delete if set as relation.)

Can we change this behavior to permanently (hard) delete the related row?

To achieve this you have to declare M2O with 2 parameters. It must be required and ondelete must be cascade. Example of Odoo

class ProductAttribute(models.Model):
    _name = "product.attribute"

    value_ids = fields.One2many('product.attribute.value', 'attribute_id', 'Values', copy=True)

class ProductAttributeValue(models.Model):
    _name = "product.attribute.value"
    attribute_id = fields.Many2one('product.attribute', string='Attribute', ondelete='cascade', required=True, index=True)