diff options
| author | Jacob Kaplan-Moss <jacob@jacobian.org> | 2008-06-08 18:13:46 +0000 |
|---|---|---|
| committer | Jacob Kaplan-Moss <jacob@jacobian.org> | 2008-06-08 18:13:46 +0000 |
| commit | 31d9dc07aa6d8c424bc84821d529cf3356e04a71 (patch) | |
| tree | 9d07cdc6668e2980f43e8586311290aefc2fefe4 /tests | |
| parent | 8398ea660373e00c8119c6a5a310c04ea7f0e680 (diff) | |
Fixed #7369: fixed a corner-case involving select_related() following non-null FKs after null ones. Thanks, George Vilches
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7597 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/regressiontests/null_fk/__init__.py | 0 | ||||
| -rw-r--r-- | tests/regressiontests/null_fk/models.py | 55 |
2 files changed, 55 insertions, 0 deletions
diff --git a/tests/regressiontests/null_fk/__init__.py b/tests/regressiontests/null_fk/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/regressiontests/null_fk/__init__.py diff --git a/tests/regressiontests/null_fk/models.py b/tests/regressiontests/null_fk/models.py new file mode 100644 index 0000000000..1bc266c033 --- /dev/null +++ b/tests/regressiontests/null_fk/models.py @@ -0,0 +1,55 @@ +""" +Regression tests for proper working of ForeignKey(null=True). Tests these bugs: + + * #7369: FK non-null after null relationship on select_related() generates an invalid query + +""" + +from django.db import models + +class SystemInfo(models.Model): + system_name = models.CharField(max_length=32) + +class Forum(models.Model): + system_info = models.ForeignKey(SystemInfo) + forum_name = models.CharField(max_length=32) + +class Post(models.Model): + forum = models.ForeignKey(Forum, null=True) + title = models.CharField(max_length=32) + + def __unicode__(self): + return self.title + +class Comment(models.Model): + post = models.ForeignKey(Post, null=True) + comment_text = models.CharField(max_length=250) + + def __unicode__(self): + return self.comment_text + +__test__ = {'API_TESTS':""" + +>>> s = SystemInfo.objects.create(system_name='First forum') +>>> 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') +>>> c2 = Comment.objects.create(comment_text='My second comment') + +# Starting from comment, make sure that a .select_related(...) with a specified +# set of fields will properly LEFT JOIN multiple levels of NULLs (and the things +# that come after the NULLs, or else data that should exist won't). +>>> c = Comment.objects.select_related().get(id=1) +>>> c.post +<Post: First Post> +>>> c = Comment.objects.select_related().get(id=2) +>>> print c.post +None + +>>> comments = Comment.objects.select_related('post__forum__system_info').all() +>>> [(c.id, c.post.id) for c in comments] +[(1, 1), (2, None)] +>>> [(c.comment_text, c.post.title) for c in comments] +[(u'My first comment', u'First Post'), (u'My second comment', None)] + +"""} |
