1

I'm trying to develop simple library app with Odoo but I get an error with manytomany relationship.

Here are classes:

class Book(models.Model):
    _name = "library.book"
    publisher_id = fields.Many2one(comodel_name="library.book.partner", string="Publisher")
    author_ids = fields.Many2many(comodel_name="library.book.partner", relation='library_book_profile_partner_authors', column1='book_id', column2='partner_id', string="Authors")
class Partner(models.Model):
    _name = "library.book.partner"
    _inherit = "res.partner"
    published_book_ids = fields.One2many("library.book", "publisher_id", string="Published Books")
    book_ids = fields.Many2many("library.book", 'partner_books', 'book_id', 'partner_id', string="Authored Books")

This is the error I always get when installing the app

TypeError: Many2many fields library.book.partner.channel_ids and res.partner.channel_ids use the same table and columns 

Someone can help to solve this please ?

Alioune
  • 11
  • 2
  • Can you add your code for the field `library.book.partner.channel_ids`? Because that's seems to be the problem and is not in your example above. – CZoellner Aug 08 '22 at 11:52
  • @CZoellner I haven't any field named channel_ids in my classes – Alioune Aug 08 '22 at 12:21

1 Answers1

1

It's the _inherit = "res.partner". Odoo is inheriting the model with a new model name which means it's creating a new table and copying all fields. For channel_ids it's trying to create the many2many "join" table, but with the same name as in res.partner. That happens because the table name is directly defined in the field (mail_channel_partner).

channel_ids = fields.Many2many('mail.channel', 'mail_channel_partner',
    'partner_id', 'channel_id', string='Channels', copy=False)

So to solve the problem you have to "redefine" channel_ids on your new model again and change the table name, for example like:

channel_ids = fields.Many2many(relation='mail_channel_library_book_partner')
CZoellner
  • 13,553
  • 3
  • 25
  • 38