On Odoo16, I'm trying to create a cron job in code like this;
self.env['ir.cron'].sudo().create({
'name': cron.cron_name,
'cron_name': cron.cron_name,
'active': False,
'interval_number': cron.interval_number,
'interval_type': cron.interval_type,
'numbercall': cron.numbercall,
'doall': cron.doall,
'model_id': cron.model_id,
'state': cron.state,
'code': cron.code.replace('{{store_id}}', str(self.id)),
})
The cron job is created and I can see that the model_id is correct and referencing my model in ir.model. However, the cron job fails because "model_name" is NULL in ir_act_server. Looking at the model, the field is a stored related field as defined below;
model_name = fields.Char(related='model_id.model', string='Model Name', readonly=True, store=True)
If would seem that the related field is not being correctly set when a new record is created with model_id correctly set.
Most modules use XML to create cron jobs. I've checked and none of these attempt to explicitly set model_name, yet in the DB, model_name is correctly set for them.
If I manually edit the server action to change the model_id through the UI, the field is correctly updated.
Has anyone else seen this issue; whether specific to ir.action.server, or any related field not being set correctly?