5

This error is thrown up when "hr.employee" or any other model with Many2many field is inherited to my model in odoo13.

Traceback (most recent call last):


File "/opt/odoo/odoo/modules/registry.py", line 59, in __new__
        return cls.registries[db_name]
      File "/opt/odoo/odoo/tools/func.py", line 69, in wrapper
        return func(self, *args, **kwargs)
      File "/opt/odoo/odoo/tools/lru.py", line 44, in __getitem__
        a = self.d[obj].me
    KeyError: 'shorepoint'

    During handling of the above exception, another exception occurred:

    Traceback (most recent call last):
      File "/opt/odoo/odoo/modules/registry.py", line 85, in new
        odoo.modules.load_modules(registry._db, force_demo, status, update_module)
      File "/opt/odoo/odoo/modules/loading.py", line 423, in load_modules
        registry.setup_models(cr)
      File "/opt/odoo/odoo/modules/registry.py", line 247, in setup_models
        model._setup_fields()
      File "/opt/odoo/odoo/models.py", line 2684, in _setup_fields
        field.setup_full(self)
      File "/opt/odoo/odoo/fields.py", line 418, in setup_full
        self._setup_regular_full(model)
      File "/opt/odoo/odoo/fields.py", line 3151, in _setup_regular_full
        raise TypeError(msg % (self, field))
    TypeError: Many2many fields school.student.category_ids and hr.employee.category_ids use the same table and columns

Here is my code:

from odoo import models, fields, api

class school_management(models.Model):
    _name = 'school.student'
    _inherit = 'hr.employee'
    _description = 'Model to manage school students'
Gokul Ram T J
  • 51
  • 1
  • 1
  • 3
  • Welcome to StackOverflow. As you may know this is a Question and Answers platform. So where is your question? :-) – CZoellner Jan 30 '20 at 09:13
  • 1
    @CZoellner He inherited `hr.employee` Odoo is confused about the `category_ids` in both models because they have the same relation name and columns he need to specify a new relation name. – Charif DZ Jan 30 '20 at 11:03
  • 1
    @CharifDZ i know what he meant, but my intention was to let him think about his "question". Maybe others can't help, because they are not as experienced as some guys like us with Odoo. A good question should always be the first aim, then good answers should follow ;-) – CZoellner Jan 30 '20 at 12:16

2 Answers2

7

In hr application the field category_ids was defined like this:

    category_ids = fields.Many2many(
          'hr.employee.category', 'employee_category_rel',
          'emp_id', 'category_id',
          string='Tags')

they specified the name of relation and columns so when he inherited the hr.employee in your custom model the same definition of category_ids is used for your model, this why Odoo is confused you used the same relation and columns to define a many2many relation for two different model. all you have to do is remove this ambiguity by specifying a new relation name for you many2many field.

class school_management(models.Model):
    _name = 'school.student'
    _inherit = 'hr.employee'
    _description = 'student' # this will be used to log message when you create a student, so keep it simple, when you create a record message will be 'student is created' 

    # define new relation name and better column names 
    # and I think you need a new category model because this one is used for employee category, may be it's better to create hr.student.category table I don't know it's up to you
    category_ids = fields.Many2many(
      'hr.employee.category', 'student_category_rel',
      'student_id', 'category_id',
      string='Tags')

I hope this helps you.

Charif DZ
  • 14,415
  • 3
  • 21
  • 40
  • So is this the procedure to handle delegation inheritance of modules with many2many fields? – Gokul Ram T J Jan 30 '20 at 13:08
  • Did it solve your problem? And not all many2many only the ones that are declared with relation name – Charif DZ Jan 30 '20 at 18:52
  • While inheriting hr.employee, yes it solved the problem. But while inheriting res.partner there are numerous many2many fields and sometimes it throws the error even if I specify the relation name. – Gokul Ram T J Jan 31 '20 at 10:45
  • 2
    You could edit your question and show me what you did, because Odoo always takes the last definition of the field. And you should see why is taking another module definition – Charif DZ Jan 31 '20 at 11:20
1

for me I had the same issue, the above comment about naming the realtion and other relations columns solved this, but after that I had to put the modules which have the realtion models in my module dependencies so it can create a new table for this relation in database.

new card
  • 13
  • 3