Search code examples
pythonodooerpodoo-14

Odoo 14 Binary field inside hr.employee.public


I have a problem with the hr.employee.public model. If I try to add a new field Char, Integer, Many2one, etc. I have no issues.

However, if I try to insert a Binary field like this employee_signature = fields.Binary(string='Employee signature', attachment=True, store=True) I always get the following error:

psycopg2.errors.UndefinedColumn: column emp.employee_signature does not exist
LINE 3: ...l,emp.priv_email,emp.employee_file_name_signature,emp.employ...

Where am I going wrong? What could be the problem?


Solution

  • If you inherit hr.employee.public to add a stored field (not x2many fields), you should see the same error message because of the init function which tries to get fields from hr.employee

    You don't need to set the store attribute to True because it's the default value (if you set store to False, you will not be able to save the attachment).

    When the attachment attribute is set to True (the default value), Odoo will set the column type of the field to None and ignore updating the database schema

    To fix this issue, you can use the same logic as image_1920 field (which is an extended Binary field) and add the field to the hr.employee model

    Example:

    class HrEmployee(models.Model):
        _inherit = 'hr.employee'
    
        employee_signature = fields.Binary(string='Employee signature')
        
    
    class HREmployeePublic(models.Model):
        _inherit = "hr.employee.public"
            
        employee_signature = fields.Binary(compute="_compute_employee_signature", compute_sudo=True)
        
        def _compute_employee_signature(self):
            for employee in self:
                employee_id = self.sudo().env['hr.employee'].browse(employee.id)
                employee.employee_signature = employee_id.employee_signature
    

    In the example above the employee signature attachment attribute is set to True and Odoo will not create a column in the employee table, this is not a problem because the same field in the public profile is a non-stored computed field so Odoo will not try to get the field from employee table

    For more details, check [IMP] hr: Introduce the public employee profile commit