﻿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
18726	Combination of F() expression with query seems to confuse sql compiler's table aliases	bugs@…	Mariusz Felisiak	"Hi,

I have a query that causes the sql compiler to output invalid sql. Running a query of the form (excuse the coarse anonymization):

{{{#!python
AThroughModel.objects.exclude ( foreignkeyfield_a__m2mfield_b__m2mfield_c = F ( ""foreignkeyfield_x__foreignkeyfield_y__foreignkeyfield_z"" ) )
}}}

this causes an exception along the lines of:

{{{
DatabaseError: missing FROM-clause entry for table ""u4""
LINE 1: ...) INNER JOIN ""m2mfield_b_join_table_name"" U5 ON (U4.""id"" = ...
}}}

Where AThroughModel is a model that's used as a ""through"" in an m2mfield from foreignkeyfield_x's model to foreignkeyfield_a's model.

Interestingly, it works when you exchange the .exclude() with a .filter().

This happens both on django 1.3.1 and 1.4.1 with a postgres backend & psycopg2.

Again, sorry about the dumb anonymization, but my boss would be a bit funny about exposing model structure. A full (non) working test case might be a bit tricky as such."	Bug	closed	Database layer (models, ORM)	dev	Normal	fixed		charette.s@…	Accepted	0	0	0	0	0	0
