From 497a0abb07ca3c8351b8e161b3ab7fc4e5653cb6 Mon Sep 17 00:00:00 2001 From: Mihir Kandoi Date: Fri, 19 Jun 2026 22:10:54 +0530 Subject: [PATCH] refactor(stock): build item-group filter via qb in item_dashboard Replace the raw EXISTS subquery (items within an item-group subtree) with frappe.qb (item_group.isin(subquery)). Same result on MariaDB; valid under Postgres' stricter SQL. Co-Authored-By: Claude Opus 4.8 (1M context) --- erpnext/stock/dashboard/item_dashboard.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/erpnext/stock/dashboard/item_dashboard.py b/erpnext/stock/dashboard/item_dashboard.py index 01615cb549d..2acf8e3bbf3 100644 --- a/erpnext/stock/dashboard/item_dashboard.py +++ b/erpnext/stock/dashboard/item_dashboard.py @@ -24,13 +24,19 @@ def get_data( filters.append(["warehouse", "=", warehouse]) if item_group: lft, rgt = frappe.db.get_value("Item Group", item_group, ["lft", "rgt"]) - items = frappe.db.sql_list( - """ - select i.name from `tabItem` i - where exists(select name from `tabItem Group` - where name=i.item_group and lft >=%s and rgt<=%s) - """, - (lft, rgt), + item = frappe.qb.DocType("Item") + item_group_dt = frappe.qb.DocType("Item Group") + items = ( + frappe.qb.from_(item) + .select(item.name) + .where( + item.item_group.isin( + frappe.qb.from_(item_group_dt) + .select(item_group_dt.name) + .where((item_group_dt.lft >= lft) & (item_group_dt.rgt <= rgt)) + ) + ) + .run(pluck="name") ) filters.append(["item_code", "in", items]) try: