From 973b928114e39814db4ccd7752c2bf9f2304bceb Mon Sep 17 00:00:00 2001 From: Akim Juillerat Date: Fri, 13 Oct 2023 12:56:18 +0200 Subject: [PATCH] Avoid error on installation with missing analytic account When installing the module account_financial_report, a relational table between account_move_line and account_analytic_account is created and computed. However, if an analytic account was used only on draft invoices before being deleted, its ID will remain in the JSON column analytic_distibution of account_move_line. In that case we get a ForeignKeyViolation because the ID doesn't exist in account_analytic_account table. Therefore, we need to check if the ID exists during the computation to avoid inserting it in the relational table and raising the error. --- account_financial_report/models/account_move_line.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/account_financial_report/models/account_move_line.py b/account_financial_report/models/account_move_line.py index 90b656cd..7150239c 100644 --- a/account_financial_report/models/account_move_line.py +++ b/account_financial_report/models/account_move_line.py @@ -19,7 +19,14 @@ class AccountMoveLine(models.Model): record.update( { "analytic_account_ids": [ - (6, 0, [int(k) for k in record.analytic_distribution]) + ( + 6, + 0, + self.env["account.analytic.account"] + .browse([int(k) for k in record.analytic_distribution]) + .exists() + .ids, + ) ] } )