diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py
index f364b1d..b7d63d3 100644
a
|
b
|
class SQLCompiler(object):
|
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 |
… |
… |
class SQLCompiler(object):
|
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, |
diff --git a/tests/regressiontests/null_fk/models.py b/tests/regressiontests/null_fk/models.py
index 4988781..1f191da 100644
a
|
b
|
Regression tests for proper working of ForeignKey(null=True).
|
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): |
… |
… |
class Comment(models.Model):
|
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') |
… |
… |
None
|
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 | """} |