[FIX] base: ValidationError for required m2o with 'set null' on_delete

Before this commit, when creating a many2one field from an xml, with an
impossible combination "`required=True` - `on_delete='set null'`", the
column was not created in the database without any notice to the user.

This commit adds a constraint checking that any attempt to create a field
with this configuration would raise a ValidationError. This constraint
replaces the previous onchange method.

closes odoo/odoo#158778

Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
This commit is contained in:
vava-odoo
2024-03-25 14:39:17 +00:00
parent ed10f6a814
commit 3a5c85fe19
+4 -4
View File
@@ -778,14 +778,14 @@ class IrModelFields(models.Model):
'message': _("The table %r if used for other, possibly incompatible fields.", self.relation_table),
}}
@api.onchange('required', 'ttype', 'on_delete')
def _onchange_required(self):
@api.constrains('required', 'ttype', 'on_delete')
def _check_on_delete_required_m2o(self):
for rec in self:
if rec.ttype == 'many2one' and rec.required and rec.on_delete == 'set null':
return {'warning': {'title': _("Warning"), 'message': _(
raise ValidationError(_(
"The m2o field %s is required but declares its ondelete policy "
"as being 'set null'. Only 'restrict' and 'cascade' make sense.", rec.name,
)}}
))
def _get(self, model_name, name):
""" Return the (sudoed) `ir.model.fields` record with the given model and name.