Search code examples
odoo

odoo inherit a model and "key error [my_database]"


I want to try to learn Odoo _inherit. But I have this error right now. And I couldn't solve the problem.

my module name is stock.info and I want the inherit product.template model.

but this error came.

TypeError: Many2many fields stock.info.taxes_id and product.template.taxes_id use the same table and columns

I added product.template to my depends in manifest too. Any advice?


Solution

  • Reminder for future questions: You should provide code for your Problem and not only an error message.

    Like the error says you have 2 many2many fields which refers to the same table and columns.

    You have to know that many2many fields in Odoo generate a mapping table in the database with two columns which are foreign keys to tables of the referenced models. The name of the mapping table and the column names are automatically generated by default. So if the automatic generation for your field creates the same name for table/columns which are already in use, you will get this error.

    To solve this, you can assign additional attributes when defining your field.

    So your field could like like this:

    taxes_ids = fields.Many2many(relation='stock_info_account_tax_rel', column1='product_id', column2='tax_id')
    

    (remember that many2many fields should be named with suffix _ids and not _id)

    Take a look at the documentation for further informations: odoo.fields.Many2many

    Parameters:

    comodel_name – name of the target model (string) mandatory except in the case of related or extended fields

    relation (str) – optional name of the table that stores the relation in the database

    column1 (str) – optional name of the column referring to “these” records in the table relation

    column2 (str) – optional name of the column referring to “those” records in the table relation

    The attributes relation, column1 and column2 are optional. If not given, names are automatically generated from model names, provided model_name and comodel_name are different!

    Note that having several fields with implicit relation parameters on a given model with the same comodel is not accepted by the ORM, since those field would use the same table. The ORM prevents two many2many fields to use the same relation parameters, except if

    both fields use the same model, comodel, and relation parameters are explicit; or

    at least one field belongs to a model with _auto = False.