Ir al contenido
Menú
Se marcó esta pregunta
5 Respuestas
18622 Vistas

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
Descartar
Mejor respuesta

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
Descartar
Mejor respuesta

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
Descartar
Autor Mejor respuesta

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
Descartar

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

Autor

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.

Publicaciones relacionadas Respuestas Vistas Actividad
1
ene 25
1491
2
sept 22
9262
2
abr 22
4267
0
jul 21
6944
1
mar 21
4700