﻿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
23605	ORM neglects to use aliases it has set up when certain multiple subqueries are used	ris	nobody	"Using django git 9d7a4ea20510a2e35fdeac21d67f3f4c17634c25

Example models.py:

{{{
from django.db import models

class ModelA ( models.Model ):
	pass

class ModelB ( models.Model ):
	modela_fk = models.ForeignKey ( ModelA )
	modelc_fk = models.ForeignKey ( ""ModelC"" )
	
	field_b0 = models.IntegerField ( null = True )
	field_b1 = models.BooleanField ()

class ModelC ( models.Model ):
	field_c0 = models.FloatField ()
}}}

Given the following query (yes, totally redundant subclauses noted):

{{{
ModelA.objects.filter (
    Q ( pk__in = ModelA.objects.filter ( Q ( modelb__field_b0__gte = 1000000 / F ( ""modelb__modelc_fk__field_c0"" ) )
        & Q ( modelb__field_b1__exact = True )
        & ~Q ( modelb__pk__in = ModelB.objects.filter (
            ~(
                Q ( field_b1__exact = True )
                & Q ( field_b0__gte = 1000000 / F ( ""modelc_fk__field_c0"" ) )
             )
        ) )
    ).filter ( modelb__field_b1__exact = True )
) )
}}}

Used against postgres 9.1 generates the error:

{{{
ProgrammingError: invalid reference to FROM-clause entry for table ""dummy_modelb""
LINE 1: ..."") AND U0.""field_b0"" IS NOT NULL)) AND V1.""id"" = (""dummy_mod...
                                                             ^
HINT:  Perhaps you meant to reference the table alias ""v1"".
}}}"	Bug	closed	Database layer (models, ORM)	1.7	Normal	fixed	orm subquery alias		Accepted	1	0	0	1	0	0
