Electronics & Programming


Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 9, 2010 20:30:12

Margie R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

SQL mystery with INNER JOIN?

I'm diving into the sql that is being generated by django and am
having trouble understanding something. Could someone give me a
hand? Say I have a Book class like this:

class Book(models.Model):
readers = models.ManyToMany('auth.user', blank=True, null=True)

Let's say for a given book object I do this:
readers = book.readers.count()

When I look at the sql via db.connection.queries, I see this:

SELECT COUNT(*) FROM auth_user INNER JOIN myapp_book_readers ON
(auth_user.id = myapp_book_readers.user_id) WHERE
myapp_book_readers.book_id` = 15

Why does the sql need to do an INNER JOIN with the auth_user table?
Why isn't the query simly this?

SELECT COUNT(*) FROM book_readers WHERE book_readers.book_id = 15

Any pointers appreciated, thanks!


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


Board footer

Moderator control

Enjoy the 12th of December

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support