From 45631cfceab6ede061be9de2845f309ef6fd80fa Mon Sep 17 00:00:00 2001 From: Alvaro Fuentes Date: Thu, 13 Jan 2022 11:21:44 +0000 Subject: [PATCH] [FIX] project: `MemoryError` on _compute_attached_docs_count When there are multiple projects and many tasks the compute of project.project.doc_count may raise a `MemoryError` on access to the projects kanban view. This issue may appear since the addition on 642fb2e6 of docs_count field to the kanban view `view_project_kanban`. https://github.com/odoo/odoo/blob/136808203545eb5d7056d941f3fc250f2871f95b/addons/project/views/project_views.xml#L613 This provokes too much information to be prefetched by the ORM, in particular the `description` fields of the tasks which may be big (e.g. contain embedded images). This issue was observed during upg-101057 closes odoo/odoo#83997 X-original-commit: 6c207ed1027d9b39eecd19886932b17f1a79dccd Signed-off-by: Laurent Stukkens (ltu) --- addons/project/models/project.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/addons/project/models/project.py b/addons/project/models/project.py index 90c88841b06..356193b8985 100644 --- a/addons/project/models/project.py +++ b/addons/project/models/project.py @@ -158,15 +158,21 @@ class Project(models.Model): return self.env['ir.config_parameter'].sudo().get_param('project.rating_status_period') or 'monthly' def _compute_attached_docs_count(self): - Attachment = self.env['ir.attachment'] + self.env.cr.execute( + """ + SELECT coalesce(task.project_id, project.id), count(*) + FROM ir_attachment attachment + LEFT JOIN project_task task ON attachment.res_model = 'project.task' AND task.id = attachment.res_id + LEFT JOIN project_project project ON attachment.res_model = 'project.project' AND project.id = attachment.res_id + WHERE project.id IN %(project_ids)s + OR task.project_id IN %(project_ids)s + GROUP BY coalesce(task.project_id, project.id) + """, + {"project_ids": tuple(self.ids)} + ) + docs_count = dict(self.env.cr.fetchall()) for project in self: - project.doc_count = Attachment.search_count([ - '|', - '&', - ('res_model', '=', 'project.project'), ('res_id', '=', project.id), - '&', - ('res_model', '=', 'project.task'), ('res_id', 'in', project.task_ids.ids) - ]) + project.doc_count = docs_count.get(project.id, 0) def _compute_task_count(self): task_data = self.env['project.task'].read_group(