From a233d534c2c4e9f1417c85ead92249a5d8c83b3a Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Thu, 2 Apr 2020 06:45:54 +0000 Subject: [FIX] core: add NOT NULL constraints in post-init The method `_init_column` for the field `alias_id` of `mail.alias.mixin` requires the reflection of models, because `mail.alias` records have a many2one field to `ir.model`. The actual initialization of the column is thus performed in post-init phase (after reflection). Therefore, the NOT NULL constraint cannot be added right away. X-original-commit: 2e3400f149ce4ed4c00bc49a519f58993b49bc51 --- odoo/fields.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'odoo/fields.py') diff --git a/odoo/fields.py b/odoo/fields.py index b96c9fe64f8..fc1a225ef34 100644 --- a/odoo/fields.py +++ b/odoo/fields.py @@ -823,11 +823,15 @@ class Field(MetaField('DummyField', (object,), {})): # the column is new or it becomes required; initialize its values if model._table_has_rows(): model._init_column(self.name) + + if self.required and not has_notnull: + # _init_column may delay computations in post-init phase + @model.pool.post_init + def add_not_null(): # flush values before adding NOT NULL constraint model.flush([self.name]) + model.pool.post_constraint(sql.set_not_null, model._cr, model._table, self.name) - if self.required and not has_notnull: - model.pool.post_constraint(sql.set_not_null, model._cr, model._table, self.name) elif not self.required and has_notnull: sql.drop_not_null(model._cr, model._table, self.name) -- cgit v1.2.3