From fd2661160e0ed851afe752f88dec065b9394eb5d Mon Sep 17 00:00:00 2001 From: AarDG10 Date: Tue, 28 Apr 2026 17:51:23 +0530 Subject: [PATCH 1/3] fix(query): always alias the table when used in joins --- frappe/database/query.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/frappe/database/query.py b/frappe/database/query.py index 9c9cefb212..08f3bce309 100644 --- a/frappe/database/query.py +++ b/frappe/database/query.py @@ -2079,8 +2079,7 @@ class LinkTableField(DynamicTableField): def _get_joined_table(self): table = frappe.qb.DocType(self.doctype) - if self.doctype == self.parent_doctype: - table = table.as_(f"tab{self.doctype}_{self.link_fieldname}") + table = table.as_(f"tab{self.doctype}_{self.link_fieldname}") return table def apply_select(self, query: QueryBuilder, engine: "Engine" = None) -> QueryBuilder: From 8b763e96e3e25df016c7a685c49e23f699577cec Mon Sep 17 00:00:00 2001 From: AarDG10 Date: Tue, 28 Apr 2026 18:23:40 +0530 Subject: [PATCH 2/3] test: fix test to accomodate multi-db queries --- frappe/tests/test_db_query.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/frappe/tests/test_db_query.py b/frappe/tests/test_db_query.py index 27f9a1b201..1a05897c9c 100644 --- a/frappe/tests/test_db_query.py +++ b/frappe/tests/test_db_query.py @@ -1238,8 +1238,14 @@ class TestDBQuery(IntegrationTestCase): fields=fields, ).get_sql() - self.assertIn("LEFT JOIN `tabSelf Linked DocType` `tabSelf Linked DocType_parent_ref`", query) - self.assertIn("LEFT JOIN `tabSelf Linked DocType` `tabSelf Linked DocType_sibling_ref`", query) + self.assertIn( + self.normalize_sql("LEFT JOIN `tabSelf Linked DocType` `tabSelf Linked DocType_parent_ref`"), + self.normalize_sql(query), + ) + self.assertIn( + self.normalize_sql("LEFT JOIN `tabSelf Linked DocType` `tabSelf Linked DocType_sibling_ref`"), + self.normalize_sql(query), + ) def test_select_star_expansion(self): count = frappe.get_list("Language", [{"SUM": 1}, {"COUNT": "*"}], as_list=1, order_by=None)[0] From 2ea2c68e6e12fdcc12a2d76723ea75ee5d7cd6f3 Mon Sep 17 00:00:00 2001 From: AarDG10 Date: Tue, 28 Apr 2026 18:45:31 +0530 Subject: [PATCH 3/3] test: fix tests to accomodate new change --- frappe/tests/test_query.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/frappe/tests/test_query.py b/frappe/tests/test_query.py index 4cf9455ef7..58f41f1278 100644 --- a/frappe/tests/test_query.py +++ b/frappe/tests/test_query.py @@ -371,10 +371,10 @@ class TestQuery(IntegrationTestCase): fields=["name"], filters={"module.app_name": "frappe"}, ).get_sql(), - "SELECT `tabDocType`.`name` FROM `tabDocType` LEFT JOIN `tabModule Def` ON `tabModule Def`.`name`=`tabDocType`.`module` WHERE `tabModule Def`.`app_name`='frappe'", + "SELECT `tabDocType`.`name` FROM `tabDocType` LEFT JOIN `tabModule Def` `tabModule Def_module` ON `tabModule Def_module`.`name`=`tabDocType`.`module` WHERE `tabModule Def_module`.`app_name`='frappe'", ) - query = "SELECT `tabDocType`.`name` FROM `tabDocType` LEFT JOIN `tabModule Def` ON `tabModule Def`.`name`=`tabDocType`.`module` WHERE `tabModule Def`.`app_name` LIKE 'frap%'" + query = "SELECT `tabDocType`.`name` FROM `tabDocType` LEFT JOIN `tabModule Def` `tabModule Def_module` ON `tabModule Def_module`.`name`=`tabDocType`.`module` WHERE `tabModule Def_module`.`app_name` LIKE 'frap%'" query = query.replace("LIKE", "ILIKE" if frappe.db.db_type == "postgres" else "LIKE") self.assertQueryEqual( frappe.qb.get_query( @@ -756,7 +756,7 @@ class TestQuery(IntegrationTestCase): "DocType", fields=["name", "module.app_name as app_name"], ).get_sql(), - "SELECT `tabDocType`.`name`,`tabModule Def`.`app_name` `app_name` FROM `tabDocType` LEFT JOIN `tabModule Def` ON `tabModule Def`.`name`=`tabDocType`.`module`", + "SELECT `tabDocType`.`name`,`tabModule Def_module`.`app_name` `app_name` FROM `tabDocType` LEFT JOIN `tabModule Def` `tabModule Def_module` ON `tabModule Def_module`.`name`=`tabDocType`.`module`", ) # fields now has strict validation, so this test is not valid anymore