Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12240 - select_related doesn't work correctly when mixing null…

…able and non-nullable keys

Thanks to embe for report and Alex for fix.



git-svn-id: http://code.djangoproject.com/svn/django/trunk@12719 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5e3a2e2f3956f358b5e82e217c20bb0b36fb0665 1 parent 40c022c
Luke Plant authored March 08, 2010
10  django/db/models/sql/compiler.py
@@ -530,10 +530,7 @@ def fill_related_selections(self, opts=None, root_alias=None, cur_depth=1,
530 530
             avoid = avoid_set.copy()
531 531
             dupe_set = orig_dupe_set.copy()
532 532
             table = f.rel.to._meta.db_table
533  
-            if nullable or f.null:
534  
-                promote = True
535  
-            else:
536  
-                promote = False
  533
+            promote = nullable or f.null
537 534
             if model:
538 535
                 int_opts = opts
539 536
                 alias = root_alias
@@ -584,10 +581,7 @@ def fill_related_selections(self, opts=None, root_alias=None, cur_depth=1,
584 581
                 next = requested.get(f.name, {})
585 582
             else:
586 583
                 next = False
587  
-            if f.null is not None:
588  
-                new_nullable = f.null
589  
-            else:
590  
-                new_nullable = None
  584
+            new_nullable = f.null or promote
591 585
             for dupe_opts, dupe_col in dupe_set:
592 586
                 self.query.update_dupe_avoidance(dupe_opts, dupe_col, alias)
593 587
             self.fill_related_selections(f.rel.to._meta, alias, cur_depth + 1,
11  tests/regressiontests/null_fk/models.py
@@ -4,7 +4,11 @@
4 4
 
5 5
 from django.db import models
6 6
 
  7
+class SystemDetails(models.Model):
  8
+    details = models.TextField()
  9
+
7 10
 class SystemInfo(models.Model):
  11
+    system_details = models.ForeignKey(SystemDetails)
8 12
     system_name = models.CharField(max_length=32)
9 13
 
10 14
 class Forum(models.Model):
@@ -30,7 +34,8 @@ def __unicode__(self):
30 34
 
31 35
 __test__ = {'API_TESTS':"""
32 36
 
33  
->>> s = SystemInfo.objects.create(system_name='First forum')
  37
+>>> d = SystemDetails.objects.create(details='First details')
  38
+>>> s = SystemInfo.objects.create(system_name='First forum', system_details=d)
34 39
 >>> f = Forum.objects.create(system_info=s, forum_name='First forum')
35 40
 >>> p = Post.objects.create(forum=f, title='First Post')
36 41
 >>> c1 = Comment.objects.create(post=p, comment_text='My first comment')
@@ -55,4 +60,8 @@ def __unicode__(self):
55 60
 >>> Comment.objects.select_related('post').filter(post__isnull=True)[0].post is None
56 61
 True
57 62
 
  63
+>>> comments = Comment.objects.select_related('post__forum__system_info__system_details')
  64
+>>> [(c.id, c.comment_text, c.post) for c in comments]
  65
+[(1, u'My first comment', <Post: First Post>), (2, u'My second comment', None)]
  66
+
58 67
 """}

0 notes on commit 5e3a2e2

Please sign in to comment.
Something went wrong with that request. Please try again.