diff --git a/frappe/database/query.py b/frappe/database/query.py index 4d3f2ddd0b..5ed3e32664 100644 --- a/frappe/database/query.py +++ b/frappe/database/query.py @@ -2087,7 +2087,8 @@ class LinkTableField(DynamicTableField): def _get_joined_table(self): table = frappe.qb.DocType(self.doctype) - table = table.as_(f"tab{self.doctype}_{self.link_fieldname}") + if self.doctype == self.parent_doctype: + table = table.as_(f"tab{self.doctype}_{self.link_fieldname}") return table def apply_select(self, query: QueryBuilder, engine: "Engine" = None) -> QueryBuilder: diff --git a/frappe/tests/test_db_query.py b/frappe/tests/test_db_query.py index 1a05897c9c..27f9a1b201 100644 --- a/frappe/tests/test_db_query.py +++ b/frappe/tests/test_db_query.py @@ -1238,14 +1238,8 @@ class TestDBQuery(IntegrationTestCase): fields=fields, ).get_sql() - 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), - ) + 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) def test_select_star_expansion(self): count = frappe.get_list("Language", [{"SUM": 1}, {"COUNT": "*"}], as_list=1, order_by=None)[0] diff --git a/frappe/tests/test_query.py b/frappe/tests/test_query.py index 18ab24c332..c413518642 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` `tabModule Def_module` ON `tabModule Def_module`.`name`=`tabDocType`.`module` WHERE `tabModule Def_module`.`app_name`='frappe'", + "SELECT `tabDocType`.`name` FROM `tabDocType` LEFT JOIN `tabModule Def` ON `tabModule Def`.`name`=`tabDocType`.`module` WHERE `tabModule Def`.`app_name`='frappe'", ) - 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 = "SELECT `tabDocType`.`name` FROM `tabDocType` LEFT JOIN `tabModule Def` ON `tabModule Def`.`name`=`tabDocType`.`module` WHERE `tabModule Def`.`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_module`.`app_name` `app_name` FROM `tabDocType` LEFT JOIN `tabModule Def` `tabModule Def_module` ON `tabModule Def_module`.`name`=`tabDocType`.`module`", + "SELECT `tabDocType`.`name`,`tabModule Def`.`app_name` `app_name` FROM `tabDocType` LEFT JOIN `tabModule Def` ON `tabModule Def`.`name`=`tabDocType`.`module`", ) # fields now has strict validation, so this test is not valid anymore