From 01b7a16ef0e3b11c7a2cbee5ec4a73251664d1c5 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sun, 13 Apr 2008 04:42:08 +0000 Subject: [PATCH] queryset-refactor: When using select_related() with an explicit foreign key, use the right join type if the FK is nullable. Fixed #6981. git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7418 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/sql/query.py | 6 ++++-- tests/regressiontests/queries/models.py | 4 ++++ 2 files changed, 8 insertions(+), 2 deletions(-) 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