﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
31963	Django: Nested OuterRef not woking to access grandparent's ID	Ashish Kulkarni	nobody	"Trying to access FeeAccount model's id using **OuterRef** in a **nested subquery**, but **not immediate parent**. Throwing:

`ValueError: This queryset contains a reference to an outer query and may only be used in a subquery.`


```
first_unpaid_fee_schedule_instalment = Subquery(FeeScheduleInstalment.objects \
	.annotate(
		total_balance = Subquery(
			FeeInstalmentTransaction.objects.filter(
				account_id=OuterRef(OuterRef('id')), # not working: need grand-parent's id here  
				instalment__schedule=OuterRef('id'), #working: parent's id
			) \
			.values('balance') \
			.annotate(total_balance=Sum('balance')) \
			.values('total_balance')
		)
	) \
	.values('id')[:1]
)


fee_accounts_with_first_unpaid = FeeAccount.objects \
	.annotate(
		first_unpaid_schedule = first_unpaid_fee_schedule_instalment,
)
```"	Bug	new	Database layer (models, ORM)	2.2	Normal		OuterRef, QuerySet, Django Filter, Annotation, Database, ORM		Unreviewed	0	0	0	0	0	0
