I have a query where it looks like Django 1.3 is generating incorrect
sql. It involves these four models.

class Aaa(models.Model):
    pass

class Bbb(models.Model):
    aaa = models.ForeignKey(Aaa, related_name='bbbs')

class Ccc(models.Model):
    bbb = models.ForeignKey(Bbb, related_name='cccs')

class Ddd(models.Model):
    aaa = models.ForeignKey(Aaa, related_name='ddds')
    passed = models.BooleanField()

So here's the query; I expect it to give me the number of cccs that
are not attached to a non-passed ddd.
I [1]: Ccc.objects.exclude(bbb__aaa__ddds__passed=False).count()

Here's the sql generated by django, from connection.queries:
SELECT COUNT(*)
FROM "myapp_ccc"
INNER JOIN "myapp_bbb" ON ("myapp_ccc"."bbb_id" = "myapp_bbb"."id")
WHERE NOT (("myapp_bbb"."aaa_id" IN (SELECT U1."id"
                                                                FROM
"myapp_bbb" U1
                                                                INNER
JOIN "myapp_aaa" U2 ON (U1."aaa_id" = U2."id")
                                                                INNER
JOIN "myapp_ddd" U3 ON (U2."id" = U3."aaa_id")
                                                                WHERE
U3."passed" = false ) AND "myapp_bbb"."aaa_id" IS NOT NULL))

It looks like the inner select statement returns a list of bbb ID's,
but the WHERE NOT condition asks if a aaa id is in the list of bbb
ID's, which doesn't make any sense. Basically, it should say:
WHERE NOT (("myapp_bbb"."id" IN (SELECT U1."id"....

What do you think? Is this a bug?

-- 
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