This question has been flagged
5 Replies
14205 Views

I wanna create a Model "PU" to optionally record details (many) of each record (one) of Model "TIN". How do I use the ORM to trigger the deletion of the records on Model "PU" when the reference to it on Model "TIN" is deleted?


Avatar
Discard
Best Answer

Hi,

Take the case of the sale order and sale order line, if you are looking to delete the order lines while deleting the sale order, you can use ondelete='cascade'

See, In the sale order line,

order_id = fields.Many2one('sale.order', string='Order Reference', required=True, ondelete='cascade', index=True, copy=False, readonly=True)


ON DELETE CASCADE referential action for a foreign key that allows you to delete data from child tables automatically when you delete the data from the parent table.


Thanks

Avatar
Discard
Best Answer

Hello Andres Fontenele,

For your case need to connect with database from "UI" or "Terminal" then execute delete query of child relation table

Hope this help you

Best Thanks,

Ankit H Gandhi.

 

Avatar
Discard
Author Best Answer

Thank you for your answer, Niyas.

I know about ondelete='cascade'. Sorry if I wasn't clear enough on my question. What I really want to know is how to delete the records when relation is lost. In your example it would be deleting the order.lines when these records aren't related to any sale.order.

Thanks.

Avatar
Discard

I think i haven't given the solution for what you have asked, with the reference lost means..., can you explain with a small example, so that it will be easy for me to understand

Author

Pretty straight forward:

Parent Model A has these fields:

ID, DESC, QUANTITY, QUANTITY_DETAILS(ONE2MANY)

Child Model B has these fields:

ID, MODEL_A_ID (MANY2ONE), DETAIL1, DETAIL2

By setting the ondelete="cascade" on MODEL_A_ID field, the record on MODEL B will be deleted if the related MODEL A record is deleted, and that is clear.

The question is, assuming that there isn't a constraint preventing me to delete the content of the MODEL_A_ID field, if I set it to NULL it means that it has lost its relation to the parent MODEL A.

So the question is how to automatically delete this record on MODEL B, if it loses the relation in the way I have described above.