From 4781deb5b602ab74da369c69e997829204c2829b Mon Sep 17 00:00:00 2001 From: Stefan Rijnhart Date: Tue, 3 Mar 2015 17:10:54 +0100 Subject: [PATCH] [FIX] base: Replace unreliable reference count through ORM by SQL When the ORM is cleaning up related attachments upon record deletion, the search() method hides the attachment of the record that is being deleted. If the same file is used exactly once in another attachment, the reference count will be 1 and the file will be deleted. --- openerp/addons/base/ir/ir_attachment.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/openerp/addons/base/ir/ir_attachment.py b/openerp/addons/base/ir/ir_attachment.py index 996b0f69c4b..6452b0d4f47 100644 --- a/openerp/addons/base/ir/ir_attachment.py +++ b/openerp/addons/base/ir/ir_attachment.py @@ -108,7 +108,9 @@ class ir_attachment(osv.osv): return fname def _file_delete(self, cr, uid, location, fname): - count = self.search(cr, 1, [('store_fname','=',fname)], count=True) + # using SQL to include files hidden through unlink or due to record rules + cr.execute("SELECT COUNT(*) FROM ir_attachment WHERE store_fname = %s", (fname,)) + count = cr.fetchone()[0] if count <= 1: full_path = self._full_path(cr, uid, location, fname) try: