From 262e00108631680a30e416c7bd6fc1d2c23d8e8f Mon Sep 17 00:00:00 2001 From: "Julien (juc) Castiaux" Date: Mon, 4 Feb 2019 09:49:40 +0000 Subject: [PATCH] [FIX] mail: delete messages on module uninstallation When uninstalling a module, the messages linked to the records of that module are not deleted leading to ghost messages when reinstalling the module. This is due to the lack of foreign keys in the definition of the chatter making it impossible to cascade delete. opw-1928208 closes odoo/odoo#30798 --- addons/mail/models/ir_model.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/addons/mail/models/ir_model.py b/addons/mail/models/ir_model.py index 0b029f3e1a1..46319ea38d4 100644 --- a/addons/mail/models/ir_model.py +++ b/addons/mail/models/ir_model.py @@ -16,9 +16,24 @@ class IrModel(models.Model): ) def unlink(self): - # Delete followers for models that will be unlinked. + # Delete followers, messages and attachments for models that will be unlinked. + models = tuple(self.mapped('model')) + query = "DELETE FROM mail_followers WHERE res_model IN %s" - self.env.cr.execute(query, [tuple(self.mapped('model'))]) + self.env.cr.execute(query, [models]) + + query = "DELETE FROM mail_message WHERE model in %s" + self.env.cr.execute(query, [models]) + + query = """ + DELETE FROM ir_attachment + WHERE res_model in %s + RETURNING store_fname + """ + self.env.cr.execute(query, [models]) + for (fname,) in self.env.cr.fetchall(): + self.env['ir.attachment']._file_delete(fname) + return super(IrModel, self).unlink() @api.multi