From e77b27ae992099564736ef0bfc16f8fa1375e95b Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Wed, 17 Jun 2026 13:19:04 +0530 Subject: [PATCH] fix(postgres): satisfy strict GROUP BY in Batch Wise Balance History report Wrap the non-aggregated, functionally-dependent column(s) in Max()/Min() (or add them to GROUP BY) so the report's grouped query is valid under PostgreSQL's strict GROUP BY. No behaviour change on MariaDB. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../batch_wise_balance_history.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/erpnext/stock/report/batch_wise_balance_history/batch_wise_balance_history.py b/erpnext/stock/report/batch_wise_balance_history/batch_wise_balance_history.py index c44dc9633ae..fb49b060fb7 100644 --- a/erpnext/stock/report/batch_wise_balance_history/batch_wise_balance_history.py +++ b/erpnext/stock/report/batch_wise_balance_history/batch_wise_balance_history.py @@ -137,7 +137,8 @@ def get_stock_ledger_entries_for_batch_no(filters): sle.item_code, sle.warehouse, sle.batch_no, - sle.posting_date, + # posting_date is constant per voucher_no (grouped) -> Max() is unchanged and postgres-valid + fn.Max(sle.posting_date).as_("posting_date"), fn.Sum(sle.actual_qty).as_("actual_qty"), fn.Sum(sle.stock_value_difference).as_("stock_value_difference"), ) @@ -182,10 +183,13 @@ def get_stock_ledger_entries_for_batch_bundle(filters): .inner_join(batch_package) .on(batch_package.parent == sle.serial_and_batch_bundle) .select( - sle.item_code, - sle.warehouse, + # item_code/warehouse/posting_date are constant per grouped voucher_no+batch_no+warehouse + # (a batch belongs to one item; warehouse mirrors the grouped batch_package.warehouse; + # a voucher has one posting_date) -> Max() is unchanged and postgres-valid + fn.Max(sle.item_code).as_("item_code"), + fn.Max(sle.warehouse).as_("warehouse"), batch_package.batch_no, - sle.posting_date, + fn.Max(sle.posting_date).as_("posting_date"), fn.Sum(batch_package.qty).as_("actual_qty"), fn.Sum(batch_package.stock_value_difference).as_("stock_value_difference"), )