Help

3

Is there a way to set a value for a field for all existing records in the database at addon installation only?

Avatar
Michael Karrer

Situation:

  1. I have an existing Database with the model event.calendar.

  2. In this Database there are already a lot of records for calendar.event

  3. I install a new addon that extends the event.calendar model with a new field "category" with required=TRUE

RESULT / ERROR:

Since the existing records do not hold any value for the new field the installation of the addon fails. Because the NOT NULL constrain could not be created since there are already records with NULL values for the new field. (Chicken Egg problem in some way)

QUESTION:

Is there a way to set a default value for this new field but just for all existing records in the Database at module (addon) installation?


Hint:

I do not want to set a permanent _default parameter for this field since after the installation is done the user should carefully set this field to a meaningful value.

Avatar
Discard
3 Answers
4
Best Answer

Hi,

To achieve your goal you need to define one method in your class where you add that new field. And set data into existing records. Please have a look on the below code.

class calendar_event(...)
    _inherit = "event.calendar"
    category_id = Fields.many2one(....)

    def _auto_init(self, cr, context=None):
        cr.execute("update event_calendar set category_id = 1 where category_id is null")

I hope it will resolve your issue.

Thanks.

1 Comment
Avatar
Discard
Avatar
Michael Karrer
-

Thank you! This was the method i was searching for!

1
Avatar
Marvin Taboada
Best Answer

Michael, `_auto_init()` is an ORM method not expected to be completely redefined because it does important stuff during module initialization, please see:

https://github.com/odoo/odoo/blob/8.0/openerp/models.py#L2416 

You can safely override this method provided you also invoke the parent's class implementation as it is done in several models from the base module, e.g.:

https://github.com/odoo/odoo/blob/8.0/openerp/addons/base/ir/ir_attachment.py#L217

    def _auto_init(self, cr, context=None):
        super(ir_attachment, self)._auto_init(cr, context)
        # Now safely perform your own stuff
        cr.execute('SELECT indexname FROM pg_indexes WHERE indexname = %s', ('ir_attachment_res_idx',))
...

The method I prefer for these cases is to add an `init(self, cr)` method on models. This method is invoked after `_auto_init()` provided it is defined in the model, see the following link for details about how both methods are invoked:

https://github.com/odoo/odoo/blob/8.0/openerp/modules/module.py#L274 

Have a nice day.

1 Comment
Avatar
Discard
Avatar
Michael Karrer
-

Thank you very much!

0
Avatar
Jérôme Thériault
Best Answer

Old question but still interresting for newer versions. In 12.0 for example,  _auto_init() and init() are fine for indexes but for NOT NULL constrains, by the time those methods are executed, it's too late and the SQL constraints have already been applied and caused a NOT NULL constraint error due to existing rows with null data. It seems the way to handle this properly is by overriding the _init_column() method, check for the column name and do the SQL UPDATE statements in there:

    @api.model_cr_context
    def _init_column(self, column_name):
        # Set a value on existing data for required fields
        if column_name == 'real_check_in':
            self.env.cr.execute("UPDATE hr_attendance SET real_check_in = check_in WHERE real_check_in IS NULL")
        elif column_name == 'real_check_out':
            self.env.cr.execute("UPDATE hr_attendance SET real_check_out = check_out WHERE real_check_out IS NULL")
        super()._init_column(column_name)


Avatar
Discard