Skip to content

OD208 invalid-one2many-inverse

A One2many's inverse field is not a Many2one.

Category Severity Series Needs the index
Correctness Error all yes

What it reports

A One2many whose inverse name, its second argument or inverse_name, is a field of the comodel of another type than Many2one. A Many2oneReference or an Integer is accepted too, since a res_model and res_id pair links records that way. The rule checks the type of the inverse field, not the model a Many2one points to.

Why it matters

Odoo accepts the definition and fails only when the field is used. It reads a One2many by searching the comodel for the records whose inverse field equals the record's id, and a line created through the One2many gets the record's id written into that field. On a field of any other type the read compares the wrong values and finds the wrong lines or none, and each line created stores an id where the field expects something else.

An inverse name the comodel does not have is reported by OD203. A One2many whose comodel no indexed module defines is not checked.

Example

models/res_partner.py
class ResPartner(models.Model):
    _inherit = "res.partner"

    signed_order_ids = fields.One2many("sale.order", "signed_by")

signed_by on sale.order is the Char holding the signer's name, not a link to the partner.

models/res_partner.py
class ResPartner(models.Model):
    _inherit = "res.partner"

    signed_order_ids = fields.One2many(
        "sale.order", "partner_id", domain=[("signed_by", "!=", False)]
    )

Suppressing

signed_order_ids = fields.One2many("sale.order", "signed_by")  # odooscan: ignore[OD208]

See suppressing a finding.