fix(postgres): satisfy strict GROUP BY in Product Bundle Balance 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) <noreply@anthropic.com>
This commit is contained in:
Mihir Kandoi
2026-06-17 13:19:06 +05:30
parent e77b27ae99
commit e69eaa5102

View File

@@ -254,11 +254,13 @@ def get_stock_ledger_entries(filters, items):
def get_item_wise_max_posting_datetime(filters, items):
"""Get the maximum Stock Ledger Entry name for the given filters and items."""
"""Get the latest posting datetime per item+warehouse for the given filters and items."""
sle = frappe.qb.DocType("Stock Ledger Entry")
query = (
frappe.qb.from_(sle)
.select(sle.item_code, sle.warehouse, sle.name, Max(sle.posting_datetime).as_("posting_datetime"))
# `name` was selected but never read by the caller (the join below only uses item_code,
# warehouse and posting_datetime); drop it so the GROUP BY is valid on postgres.
.select(sle.item_code, sle.warehouse, Max(sle.posting_datetime).as_("posting_datetime"))
.where(sle.item_code.isin(items) & (sle.is_cancelled == 0))
.groupby(sle.item_code, sle.warehouse)
)