diff options
| author | Raphael Collet <rco@odoo.com> | 2019-12-03 13:33:58 +0000 |
|---|---|---|
| committer | Raphael Collet <rco@odoo.com> | 2019-12-04 08:46:08 +0000 |
| commit | 51d020e5fa82ca4d935fe2420711b8365b5e5621 (patch) | |
| tree | 96ef3c59f0c3ea6a7bc08e776b2b7c45ff1a9fc7 | |
| parent | bf250a33f0aca36f1c354d163219680fc504931a (diff) | |
[FIX] models: query blowup when unlinking many records
The method `unlink` checks whether any of the records to delete is used
as the default value of a company-dependent field. The query simply
blows up when deleting several millions of records. The fix consists in
performing the check in batches.
closes odoo/odoo#41331
X-original-commit: 30e8b8f2554e5d16b19f16ea90e3503098dc98a0
Signed-off-by: Raphael Collet (rco) <rco@openerp.com>
| -rw-r--r-- | odoo/models.py | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/odoo/models.py b/odoo/models.py index ddc6a518acc..de377196184 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -3292,19 +3292,13 @@ Record ids: %(records)s self.flush() self.modified(self._fields) - # Check if the records are used as default properties. - refs = ['%s,%s' % (self._name, i) for i in self.ids] - if self.env['ir.property'].search([('res_id', '=', False), ('value_reference', 'in', refs)]): - raise UserError(_('Unable to delete this document because it is used as a default property')) - - # Delete the records' properties. with self.env.norecompute(): self.check_access_rule('unlink') - self.env['ir.property'].search([('res_id', 'in', refs)]).sudo().unlink() cr = self._cr Data = self.env['ir.model.data'].sudo().with_context({}) Defaults = self.env['ir.default'].sudo() + Property = self.env['ir.property'].sudo() Attachment = self.env['ir.attachment'].sudo() ir_model_data_unlink = Data ir_attachment_unlink = Attachment @@ -3317,6 +3311,14 @@ Record ids: %(records)s self.env.remove_to_compute(field, self) for sub_ids in cr.split_for_in_conditions(self.ids): + # Check if the records are used as default properties. + refs = ['%s,%s' % (self._name, i) for i in sub_ids] + if Property.search([('res_id', '=', False), ('value_reference', 'in', refs)], limit=1): + raise UserError(_('Unable to delete this document because it is used as a default property')) + + # Delete the records' properties. + Property.search([('res_id', 'in', refs)]).unlink() + query = "DELETE FROM %s WHERE id IN %%s" % self._table cr.execute(query, (sub_ids,)) |
