This is a pretty good test case, but the looks of it. The only thing
missing is the custom manager (since you're calling MyItem.objects_all,
not MyItem.objects) and that possibly affects something, too.
In any case, any time we're generating invalid SQL instead of raising
some other error (or doing the right thing for valid code -- and the
above looks valid at first glance), it's a bug.
Could you please open a ticket for this so that I remember to look at it
(assign it to "mtredinnick" straight away, if you like).
Well, not really. The users_myitem table in the main query and
users_myitem in the subquery are different instances of the table, so it
has to have an alias. The main table from the outer query is always
going to end up being "U0" in an inner query like this (since it's the
first in a list of names and the number is the index in the list).
> and then is referred to as 'U1' on the next line.
The interesting bit is what happened to U1. I have a suspicion this
might be related to another optimisation that isn't working reliably,
but which I thought wasn't harming anything (just generating less than
optimal code): we shouldn't really need U0 in the above query, since we
can just test against the thing it's joining to.
There's definitely something going wrong here. Again, we shouldn't ever
be sending malformed SQL to the database. That's always a Django bug. I
can't drop everything right this minute to look at it, but if you open a
ticket I'll certainly take a look, since SQL bugs are something I take
very personally.
Regards,
Malcolm