From 6c507a5cd4e1f1752ce163023000065842764274 Mon Sep 17 00:00:00 2001 From: Luke Plant Date: Mon, 8 Mar 2010 12:59:50 +0000 Subject: [PATCH] [1.1.X] Fixed #12240 - select_related doesn't work correctly when mixing nullable and non-nullable keys Thanks to embe for report and Alex for fix. Backport of 12719 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12720 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/sql/query.py | 10 ++-------- tests/regressiontests/null_fk/models.py | 11 ++++++++++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 36a83036a9..6b9f4b6d56 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -1361,10 +1361,7 @@ class BaseQuery(object): avoid = avoid_set.copy() dupe_set = orig_dupe_set.copy() table = f.rel.to._meta.db_table - if nullable or f.null: - promote = True - else: - promote = False + promote = nullable or f.null if model: int_opts = opts alias = root_alias @@ -1415,10 +1412,7 @@ class BaseQuery(object): next = requested.get(f.name, {}) else: next = False - if f.null is not None: - new_nullable = f.null - else: - new_nullable = None + new_nullable = f.null or promote for dupe_opts, dupe_col in dupe_set: self.update_dupe_avoidance(dupe_opts, dupe_col, alias) self.fill_related_selections(f.rel.to._meta, alias, cur_depth + 1, diff --git a/tests/regressiontests/null_fk/models.py b/tests/regressiontests/null_fk/models.py index 49887819ac..1f191daaad 100644 --- a/tests/regressiontests/null_fk/models.py +++ b/tests/regressiontests/null_fk/models.py @@ -4,7 +4,11 @@ Regression tests for proper working of ForeignKey(null=True). from django.db import models +class SystemDetails(models.Model): + details = models.TextField() + class SystemInfo(models.Model): + system_details = models.ForeignKey(SystemDetails) system_name = models.CharField(max_length=32) class Forum(models.Model): @@ -30,7 +34,8 @@ class Comment(models.Model): __test__ = {'API_TESTS':""" ->>> s = SystemInfo.objects.create(system_name='First forum') +>>> d = SystemDetails.objects.create(details='First details') +>>> s = SystemInfo.objects.create(system_name='First forum', system_details=d) >>> f = Forum.objects.create(system_info=s, forum_name='First forum') >>> p = Post.objects.create(forum=f, title='First Post') >>> c1 = Comment.objects.create(post=p, comment_text='My first comment') @@ -55,4 +60,8 @@ None >>> Comment.objects.select_related('post').filter(post__isnull=True)[0].post is None True +>>> comments = Comment.objects.select_related('post__forum__system_info__system_details') +>>> [(c.id, c.comment_text, c.post) for c in comments] +[(1, u'My first comment', ), (2, u'My second comment', None)] + """}