From 3ffc61f8fa93237ee6c41001795c5397f0d055da Mon Sep 17 00:00:00 2001 From: "Aurelien van Delft (avd)" Date: Tue, 14 Mar 2023 13:17:38 +0000 Subject: [PATCH] [FIX] product_expiry: fix MemoryError on module installation. In large databases with > 2M stock_move_lines, installing the product_expiry module can raise a MemoryError because the field_cache is overfilled by the computation of the expiration_date computed stored field. Adding an _auto_init for this field prevent such errors from being raised by creating the column manually. closes odoo/odoo#116456 X-original-commit: 8b82d3154858582f6d321bd6473557fc192af25f Signed-off-by: William Henrotin (whe) --- addons/product_expiry/models/stock_move_line.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/addons/product_expiry/models/stock_move_line.py b/addons/product_expiry/models/stock_move_line.py index 78699bbb55b..a81e67cc61b 100644 --- a/addons/product_expiry/models/stock_move_line.py +++ b/addons/product_expiry/models/stock_move_line.py @@ -4,6 +4,7 @@ import datetime from odoo import api, fields, models +from odoo.tools.sql import column_exists, create_column class StockMoveLine(models.Model): @@ -17,6 +18,16 @@ class StockMoveLine(models.Model): use_expiration_date = fields.Boolean( string='Use Expiration Date', related='product_id.use_expiration_date') + def _auto_init(self): + """ Create column for 'expiration_date' here to avoid MemoryError when letting + the ORM compute it after module installation. Since both 'lot_id.expiration_date' + and 'product_id.use_expiration_date' are new fields introduced in this module, + there is no need for an UPDATE statement here. + """ + if not column_exists(self._cr, "stock_move_line", "expiration_date"): + create_column(self._cr, "stock_move_line", "expiration_date", "timestamp") + return super()._auto_init() + @api.depends('product_id', 'picking_type_use_create_lots', 'lot_id.expiration_date') def _compute_expiration_date(self): for move_line in self: