title: Raw query must include the primary key
body: <p>I got a raw SQL statement in my <code>views.py</code></p>
  
  <pre><code>Message.objects.raw('''
          SELECT s1.ID, s1.CHARACTER_ID, MAX(s1.MESSAGE) MESSAGE, MAX(s1.c) occurrences
          FROM
             (SELECT ID, CHARACTER_ID, MESSAGE, COUNT(*) c
              FROM tbl_message WHERE ts &gt; DATE_SUB(NOW(), INTERVAL %s DAY) GROUP BY CHARACTER_ID,MESSAGE) s1
          LEFT JOIN
             (SELECT ID, CHARACTER_ID, MESSAGE, COUNT(*) c
              FROM tbl_message WHERE ts &gt; DATE_SUB(NOW(), INTERVAL %s DAY) GROUP BY CHARACTER_ID,MESSAGE) s2
            ON s1.CHARACTER_ID=s2.CHARACTER_ID
           AND s1.c &lt; s2.c
          WHERE s2.c IS NULL
          GROUP BY CHARACTER_ID
          ORDER BY occurrences DESC''', [days, days])
  </code></pre>
  
  <p>The result of this SQL statement (tested on database directly) is:</p>
  
  <pre><code>ID  | CHARACTER_ID | MESSAGE | OCCURENCES
  ----+--------------+---------+--------------
  148 | 10           | test    | 133
  </code></pre>
  
  <p>But all I got is a <code>InvalidQuery</code> Exception with the information <code>Raw query must include the primary key</code></p>
  
  <p>Then I double checked the <a href="https://docs.djangoproject.com/en/dev/topics/db/sql/#deferring-model-fields" rel="noreferrer">docs</a> and read: </p>
  
  <blockquote>
    <p>There is only one field that you can’t leave out - the primary key
    field....An InvalidQuery exception will be raised if you forget to include the primary key.</p>
  </blockquote>
  
  <p>As you can see I got the requested primary key added in my statement. What's wrong?</p>
  
  <pre><code>class Message(models.Model):
      character = models.ForeignKey('Character')
      message = models.TextField()
      location = models.ForeignKey('Location')
      ts = models.DateTimeField()
  
      class Meta:
          pass
  
      def __unicode__(self):
          return u'%s: %s...' % (self.character, self.message[0:20])
  </code></pre>