diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 34b3c530b3..80ec236edb 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -793,11 +793,13 @@ class Query(object): lhs_col = int_opts.parents[int_model].column int_opts = int_model._meta alias = self.join((alias, int_opts.db_table, lhs_col, - int_opts.pk.column), exclusions=used) + int_opts.pk.column), exclusions=used, + promote=f.null) else: alias = root_alias alias = self.join((alias, table, f.column, - f.rel.get_related_field().column), exclusions=used) + f.rel.get_related_field().column), exclusions=used, + promote=f.null) used.add(alias) self.select.extend([(alias, f2.column) for f2 in f.rel.to._meta.fields]) diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py index 39f13d616d..f80d1eb827 100644 --- a/tests/regressiontests/queries/models.py +++ b/tests/regressiontests/queries/models.py @@ -561,6 +561,10 @@ Multiple filter statements are joined using "AND" all the time. >>> Author.objects.filter(Q(extra__note=n1)|Q(item__note=n3)).filter(id=a1.id) [] +Bug #6981 +>>> Tag.objects.select_related('parent').order_by('name') +[, , , , ] + Bug #6180, #6203 >>> Item.objects.count() 4