﻿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
18375	F() doesn't work as expected across multijoin relations	FunkyBob	Anssi Kääriäinen	"When trying to compare a field to another on the same related record, F() will compare against a separate alias of the table, thus not guaranteeing filtering against the same row.

There doesn't appear to be anything in the docs or tests to indicate what the 'correct' behavior is.  Also, there's no apparent way to control it.

Using the attached models, I get the following:

{{{

(InteractiveConsole)
>>> from sample import models
>>> from django.db.models import Q, F
>>> qset = models.Uber.objects.filter(unter__available__gt=F('unter__used'))
>>> str(qset.query)
'SELECT ""sample_uber"".""id"", ""sample_uber"".""title"" FROM ""sample_uber"" INNER JOIN ""sample_unter"" ON (""sample_uber"".""id"" = ""sample_unter"".""uber_id"") INNER JOIN ""sample_unter"" T3 ON (""sample_uber"".""id"" = T3.""uber_id"") WHERE T3.""available"" >  ""sample_unter"".""used""'
}}}

The SQL nicely formatted shows:
{{{
SELECT ""sample_uber"".""id"", ""sample_uber"".""title""
FROM ""sample_uber""
INNER JOIN ""sample_unter"" ON (""sample_uber"".""id"" = ""sample_unter"".""uber_id"")
INNER JOIN ""sample_unter"" T3 ON (""sample_uber"".""id"" = T3.""uber_id"")
WHERE T3.""available"" >  ""sample_unter"".""used""
}}}

So, the `F('unter__used')` is using the 'sample_unter' join, whereas the `unter__available__gt` is using the T3 alias.
"	Bug	closed	Database layer (models, ORM)	1.4	Release blocker	fixed		German M. Bravo	Accepted	1	0	0	0	0	0
