Author: lukeplant Date: 2010-03-08 06:47:16 -0600 (Mon, 08 Mar 2010) New Revision: 12719
Modified: django/trunk/django/db/models/sql/compiler.py django/trunk/tests/regressiontests/null_fk/models.py Log: Fixed #12240 - select_related doesn't work correctly when mixing nullable and non-nullable keys Thanks to embe for report and Alex for fix. Modified: django/trunk/django/db/models/sql/compiler.py =================================================================== --- django/trunk/django/db/models/sql/compiler.py 2010-03-08 06:06:57 UTC (rev 12718) +++ django/trunk/django/db/models/sql/compiler.py 2010-03-08 12:47:16 UTC (rev 12719) @@ -530,10 +530,7 @@ 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 @@ -584,10 +581,7 @@ 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.query.update_dupe_avoidance(dupe_opts, dupe_col, alias) self.fill_related_selections(f.rel.to._meta, alias, cur_depth + 1, Modified: django/trunk/tests/regressiontests/null_fk/models.py =================================================================== --- django/trunk/tests/regressiontests/null_fk/models.py 2010-03-08 06:06:57 UTC (rev 12718) +++ django/trunk/tests/regressiontests/null_fk/models.py 2010-03-08 12:47:16 UTC (rev 12719) @@ -4,7 +4,11 @@ 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 @@ __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 @@ >>> 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', <Post: First Post>), (2, u'My second comment', None)] + """} -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-upda...@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.