select_related handling with Oracle is (likely) broken
|Reported by:||Malcolm Tredinnick||Owned by:||Malcolm Tredinnick|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||Ian Kelly||Triage Stage:||Unreviewed|
|Has patch:||no||Needs documentation:|
|Needs tests:||Patch needs improvement:|
I don't have any way to test this right now, but whilst fixing #7813, I realised there's some broken code in
Query.results_iter. We have these lines
if resolve_columns: if self.select_fields: fields = self.select_fields + self.related_select_fields else: fields = self.model._meta.fields
(starting at line 200 in r8053). The problem is that
self.related_select_fields isn't populated until
pre_sql_setup() is called by
as_sql(), which is called as part of the
execute_sql() call lower down in that method. Thus, when we are querying for specific fields plus something using
select_related(), we're going to be using an empty list as the second piece of the RHS, when it's really just a list that hasn't been populated yet.
I haven't really worked out how to fix this yet. Noting it so that I don't forget to do so later. Calling
pre_sql_setup() earlier looks fragile. Maybe we have to populate the resolve-columns
fields list inside the loop if it hasn't been populated yet (so on the first iteration), or something like that.
We also need to write a test for this. I can't immediately see how that branch is triggered, but that could be because it's late right at the moment.
(ikelly: adding you to the CC in case you have any good ideas here. Feel free to remove yourself, of course, if you're not interested.)