[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
This commit is contained in:
Julien (juc) Castiaux
2019-02-04 14:16:47 +00:00
parent 1746112794
commit 262e001086
+17 -2
View File
@@ -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