'Self referencing relationship in the fleet model

In the fleet management models, in the fleet.vehicle.cost model, we have these two fields:

 'parent_id': fields.many2one('fleet.vehicle.cost', 'Parent', help='Parent cost to this current cost'),
 'cost_ids': fields.one2many('fleet.vehicle.cost', 'parent_id', 'Included Services'),

Now, for the first one I understand its logic: a cost may have a parent cost. But I don't understand the second relation and how it works and why I don't find this field in the postgresql database.

Can you explain to me this please?



Solution 1:[1]

  1. Many2One: The system creates a field in a model/table in which a link to the associated model/table. In your example system create parent_id field into fleet.vehicle.cost model (Database table fleet_vehicle_cost).
  2. One2Many: First need Many2one for setup One2many relationship. Because in the database there is no any special thing happen when we add One2Many field.

So, How it works:

When system try to get the records of One2many field then it will get basd on Many2one field.

For exmaple sale_order and sale_order_line

Model (sale.order.line)
order_id = fields.Many2one('sale.order')


Model (sale.order)
line_ids = fields.One2many('sale.order.line', 'order_id')

So, For example we tried to open sales order it has id 7. For getting data for sales order line system will get order_id field under sale_order_line model have value 7.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Synodica Solutions Pvt. Ltd.