#9166: stupid SQL
------------------------------------------+---------------------------------
Reporter: [EMAIL PROTECTED] | Owner: nobody
Status: new | Milestone: post-1.0
Component: Database layer (models, ORM) | Version: 1.0
Keywords: STUPID SQL | Stage: Unreviewed
Has_patch: 0 |
------------------------------------------+---------------------------------
Django orm does not follow reverse relations. Why not?
I present example how this could be done:
{{{
#models:
from django.db import models
class Book(models.Model):
ISBN = models.IntegerField()
class Translation(models.Model):
title = models.CharField(max_length=32)
book = models.ForeignKey("Book")
language= models.CharField(max_length=4)
#views:
def index(request):
books =
Book.objects.filter(translation__language="en",translation__title="1")
output = ', '.join([obj.translation_set.all()[0].title for obj in
books])
return HttpResponse(output)
#what django does:SQL
SELECT "test3_book"."id", "test3_book"."ISBN" FROM "test3_book" INNER JOIN
"test3_translation" ON ("test3_book"."id" = "test3_translation"."book_id")
WHERE ("test3_translation"."language" <> 'bz' AND
"test3_translation"."title" <> 'fdsfsd' )
SELECT "test3_translation"."id", "test3_translation"."title",
"test3_translation"."book_id", "test3_translation"."language" FROM
"test3_translation" WHERE "test3_translation"."book_id" = 1 LIMIT 1
# so if we have 100 objects it will do 100 additional queries
#change proposed :
#this would be just fine (just added Translation fields):
SELECT
"test3_book"."id",
"test3_book"."ISBN",
"test3_translation"."title", #this was added
"test3_translation"."language" #this was added
FROM "test3_book"
INNER JOIN "test3_translation"
ON ("test3_book"."id" = "test3_translation"."book_id")
WHERE ("test3_translation"."language" <> 'bz' AND
"test3_translation"."title" <> 'fdsfsd' )
}}}
this means that if I will get 100 models, djanog will hit db 100 more
times!
this can be avoided by adding the extra fields from related model to
SELECT
Note that this is only example, what if Book has more than 1 related
object, fetching by Translation makes no sense.
--
Ticket URL: <http://code.djangoproject.com/ticket/9166>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---