From 3a5c85fe199086644e6bc8bdeef9e30306fc23fd Mon Sep 17 00:00:00 2001 From: vava-odoo Date: Fri, 22 Mar 2024 10:09:12 +0100 Subject: [PATCH] [FIX] base: ValidationError for required m2o with 'set null' on_delete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- odoo/addons/base/models/ir_model.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/odoo/addons/base/models/ir_model.py b/odoo/addons/base/models/ir_model.py index e01202bff08..7e48c328a58 100644 --- a/odoo/addons/base/models/ir_model.py +++ b/odoo/addons/base/models/ir_model.py @@ -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.