Django

Code

Ticket #8634 (closed: fixed)

Opened 2 years ago

Last modified 2 years ago

Wrong ordering by a field in a different table example in new (development ) queryset-api documentation

Reported by: Jerzy.Ludwichowski@uni.torun.pl Assigned to: ramiro
Milestone: 1.0 Component: Documentation
Version: SVN Keywords:
Cc: Triage Stage: Accepted
Has patch: 1 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

The example on ordering a queryset by a field in a different table in the development documentation on queryset-api (http://docs.djangoproject.com/en/dev/ref/models/querysets/#queryset-api) offers the following syntax:

Entry.objects.order_by('blogs_blog.name', 'headline')

preceded by the following explanation: "To order by a field in a different table, add the other table’s name and a dot, like so:"

For the following model definitions

from django.db import models

class Poll(models.Model):
    question = models.CharField(max_length=200)
    pub_date = models.DateTimeField('date published')
    
    def __unicode__(self):
        return self.question


class Choice(models.Model):
    poll = models.ForeignKey(Poll)
    choices = models.CharField(max_length=200)
    votes = models.IntegerField()
    
    def __unicode__(self):
        return self.choices

with MySQL, for this retrieval example:

c=Choice.objects.order_by("polls_poll.question",'-votes')
list(c)

the result is "OperationalError?: (1054, "Unknown column 'polls_poll.question' in 'order clause'" in reply to the following SQL query generated:

SELECT `polls_choice`.`id`, `polls_choice`.`poll_id`, `polls_choice`.`choices`,
`polls_choice`.`votes` FROM `polls_choice` ORDER BY `polls_poll`.question ASC,
 `polls_choice`.`votes` DESC

On the other hand if one follows the syntax offered in the "mainstream" documentation (http://www.djandoproject.com/db-api), i.e.,

c=Choice.objects.order_by("poll__question",'-votes')
list(c) 

things do work, with the following SQL query generated:

SELECT `polls_choice`.`id`, `polls_choice`.`poll_id`, `polls_choice`.`choices`,
`polls_choice`.`votes` FROM `polls_choice` INNER JOIN `polls_poll` 
ON (`polls_choice`.`poll_id` = `polls_poll`.`id`) ORDER BY `polls_poll`.`question` ASC, 
`polls_choice`.`votes` DESC

This problem might be related to the one reported in the accepted ticket #2884 (component: Admin interface), solution for which is said to be deferred until after queryset-refactor.

Attachments

t8634.diff (2.6 kB) - added by ramiro on 08/28/08 14:08:39.

Change History

08/28/08 06:11:01 changed by Jerzy.Ludwichowski@uni.torun.pl

  • needs_better_patch changed.
  • needs_tests changed.
  • needs_docs changed.

Further to the report: I'm running on the development code (rev 8649).

08/28/08 06:24:31 changed by ramiro

  • owner changed from nobody to ramiro.
  • status changed from new to assigned.
  • milestone set to 1.0.

Correct. This is a regression caused by the landing of the documentation refactor (doc-rf) in trunk [8506] The pre doc-rf documentation about this topic is correct: http://www.djangoproject.com/documentation/db-api/#order-by-fields

Accepting the ticket and targeting it to 1.0.

08/28/08 09:47:01 changed by jacob

  • stage changed from Unreviewed to Accepted.

08/28/08 14:08:39 changed by ramiro

  • attachment t8634.diff added.

08/28/08 14:08:58 changed by ramiro

  • has_patch set to 1.

08/29/08 00:06:25 changed by mtredinnick

  • status changed from assigned to closed.
  • resolution set to fixed.

Fixed in [8694]


Add/Change #8634 (Wrong ordering by a field in a different table example in new (development ) queryset-api documentation)




Change Properties
Action