Opened 10 years ago

Closed 10 years ago

#22923 closed Bug (duplicate)

order_by clause missing on nested queryset

Reported by: Grant Jenks Owned by: nobody
Component: Database layer (models, ORM) Version: 1.6
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The ORDER BY clause is getting dropped on a nested queryset with the sqlite3 database backend.

Repro steps:

models.py

from django.db import models

class Report(models.Model):
    modify_time = models.DateTimeField(auto_now=True)
    uuid = models.CharField(max_length=36, unique=True)
    deleted = models.BooleanField(default=False)

class Measure(models.Model):
    report = models.ForeignKey(Report, db_column='report_uuid', to_field='uuid', on_delete=models.CASCADE)

python manage.py shell

In [1]: from testtest.models import Report, Measure

In [2]: reports = Report.objects.filter(deleted=False).order_by('-modify_time')[:5]

In [3]: print reports.query
SELECT "deleteme_report"."id", "deleteme_report"."modify_time", "deleteme_report"."uuid", "deleteme_report"."deleted" FROM "deleteme_report" WHERE "deleteme_report"."deleted" = False  ORDER BY "deleteme_report"."modify_time" DESC LIMIT 5

In [4]: measures = Measure.objects.filter(report__in=reports)

In [5]: print measures.query
SELECT "deleteme_measure"."id", "deleteme_measure"."report_uuid" FROM "deleteme_measure" WHERE "deleteme_measure"."report_uuid" IN (SELECT "deleteme_report"."uuid" FROM "deleteme_report" WHERE "deleteme_report"."deleted" = False  LIMIT 5)

Note that reports.query includes the ORDER BY clause but when nested in measures.query, it is missing.

settings.py is stock from django-admin.py in v1.6 on CPython 2.7.

Change History (2)

comment:1 by Grant Jenks, 10 years ago

Component: UncategorizedDatabase layer (models, ORM)

Just tested with 1.7rc1 and this doesn't seem to be an issue:

> python manage.py shell
Python 2.7.7 (default, Jun  2 2014, 01:41:14) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from testtest.models import Report, Measure
>>> reports = Report.objects.filter(deleted=False).order_by('-modify_time')[:5]
>>> print reports.query
SELECT "testtest_report"."id", "testtest_report"."modify_time", "testtest_report"."uuid", "testtest_report"."deleted" FROM "testtest_report" WHERE "testtest_report"."deleted" = False ORDER BY "testtest_report"."modify_time" DESC LIMIT 5
>>> measures = Measure.objects.filter(report__in=reports)
>>> print measures.query
SELECT "testtest_measure"."id", "testtest_measure"."report_uuid" FROM "testtest_measure" WHERE "testtest_measure"."report_uuid" IN (SELECT U0."uuid" FROM "testtest_report" U0 WHERE U0."deleted" = False ORDER BY U0."modify_time" DESC LIMIT 5)

Does anyone know what changed might've fixed this?

comment:2 by Simon Charette, 10 years ago

Resolution: duplicate
Status: newclosed

I think this is a duplicate of #22434 which has been fixed in 1.7.x

Note: See TracTickets for help on using tickets.
Back to Top