Querying for .count() on a result (len(results) will also trigger the issue) that pulls in a FK table throws off the count. Sample code, tests and results below:

foo/models.py
=============
from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=42)
    # other fields

class Alias(models.Model):
    person = models.ForeignKey(Person,
        related_name="aliases",
        )
    alias = models.CharField(
        max_length=40,
        )
    class Meta:
        unique_together=(
            ("person", "alias"),
            )


foo/tests.py
============
from django.test import TestCase
from django.db.models import Q
import foo.models as m

class SimpleTest(TestCase):
    def test_basic_addition(self):
        p = m.Person(name="William")
        p.save()
        aliases = ["Wil", "Will", "Bill", "Billy", "Willy", "Bubba"]
        for alias in aliases:
            a = m.Alias(person=p, alias=alias)
            a.save()
        for i, alias in enumerate(aliases + ["William"]):
            results = m.Person.objects.filter(
                Q(name__iexact=alias) |
                Q(aliases__alias__iexact=alias)
                )
            self.assertEqual(1, results.count(),
                "Failed #%i on %r" % (i, alias),)
            #self.assertEqual(1, len(list(results)),
            #    "Failed #%i on %r" % (i, alias),)


running "./manage.py test foo" fails with this:

Creating test database for alias 'default'...
F
======================================================================
FAIL: test_basic_addition (prj.foo.tests.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/tim/code/dev/foo/tests.py", line 15, in test_basic_addition
    Q(aliases__alias__iexact="William")
AssertionError: 1 != 6 : Failed on #6 'William'

----------------------------------------------------------------------
Ran 1 test in 0.004s

FAILED (failures=1)
Destroying test database for alias 'default'...



I understand what it's doing at a SQL level, but not sure if this is a "correct" behavior for .count() or the resulting list of objects.

Any thoughts/suggestions?

-tkc





--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to