[Django] #24174: Extra order by ignores descending orderings

6 views
Skip to first unread message

Django

unread,
Jan 18, 2015, 1:44:28 PM1/18/15
to django-...@googlegroups.com
#24174: Extra order by ignores descending orderings
----------------------------------------------+-----------------------
Reporter: BertrandBordage | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.8alpha1
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+-----------------------
Consider this simple model:

{{{
class Test(Model):
name = CharField(max_length=50)
class Meta(object):
db_table = 'test'
}}}

{{{Test.objects.extra(order_by=['-name'])}}} should be converted to
something like {{{SELECT * FROM test ORDER BY name DESC}}}, but instead it
becomes {{{SELECT * FROM test ORDER BY name ASC}}}. The minus is ignored.

It wasn’t happening in Django 1.6 and 1.7.

(This issue was discovered in [https://github.com/BertrandBordage/django-
cachalot/blob/c578cbff8a931d8bec3c3e026c8908cb61bf71d4/cachalot/tests/read.py#L495-501
the test suite of django-cachalot])

--
Ticket URL: <https://code.djangoproject.com/ticket/24174>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 18, 2015, 4:25:41 PM1/18/15
to django-...@googlegroups.com
#24174: Extra order by ignores descending orderings
-------------------------------------+-------------------------------------
Reporter: BertrandBordage | Owner: jarshwah
Type: Bug | Status: assigned
Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jarshwah):

* status: new => assigned
* needs_better_patch: => 0
* needs_tests: => 0
* owner: nobody => jarshwah
* needs_docs: => 0
* stage: Unreviewed => Accepted


Comment:

Based on looking at the models of the linked test suite I'm guessing that
this is hitting the "duplicate" order by clause. It's keeping the default
ordering of the model, which is by name ascending, and removing the extra
which is by name descending. Extra ordering should prevent the default
ordering from being applied, and the negative sign should be preserved.

--
Ticket URL: <https://code.djangoproject.com/ticket/24174#comment:1>

Django

unread,
Jan 18, 2015, 5:49:55 PM1/18/15
to django-...@googlegroups.com
#24174: Extra order by ignores descending orderings
-------------------------------------+-------------------------------------
Reporter: BertrandBordage | Owner: jarshwah
Type: Bug | Status: assigned
Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by BertrandBordage):

I tested without default ordering, the issue is still there.

But I wasn’t precise enough in my report, it’s only happening when we
specify the table name:
{{{
>>> print Test.objects.all().query
SELECT […] FROM "test"
>>> print Test.objects.extra(order_by=['-name']).query
SELECT […] FROM "test" ORDER BY "test"."name" DESC
>>> print Test.objects.extra(order_by=['-test.name']).query
SELECT […] FROM "test" ORDER BY ("test".name) ASC
>>> print Test.objects.extra(order_by=['-"test"."name"']).query
SELECT […] FROM "test" ORDER BY ("test"."name") ASC
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24174#comment:2>

Django

unread,
Jan 18, 2015, 5:56:40 PM1/18/15
to django-...@googlegroups.com
#24174: Extra order by ignores descending orderings
-------------------------------------+-------------------------------------
Reporter: BertrandBordage | Owner: jarshwah
Type: Bug | Status: assigned
Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by jarshwah):

Thanks for the follow up. That makes it even easier to track down.

https://github.com/django/django/blob/6e13c0490d67cdf210411f08feca3b78a49645ea/django/db/models/sql/compiler.py#L259

This is the block responsible, and should be fairly trivial to fix.

--
Ticket URL: <https://code.djangoproject.com/ticket/24174#comment:3>

Django

unread,
Jan 18, 2015, 6:39:54 PM1/18/15
to django-...@googlegroups.com
#24174: Extra order by ignores descending orderings
-------------------------------------+-------------------------------------
Reporter: BertrandBordage | Owner: jarshwah
Type: Bug | Status: assigned
Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by jarshwah):

* has_patch: 0 => 1
* stage: Accepted => Ready for checkin


Comment:

https://github.com/django/django/pull/3947

--
Ticket URL: <https://code.djangoproject.com/ticket/24174#comment:4>

Django

unread,
Jan 18, 2015, 8:53:24 PM1/18/15
to django-...@googlegroups.com
#24174: Extra order by ignores descending orderings
-------------------------------------+-------------------------------------
Reporter: BertrandBordage | Owner: jarshwah
Type: Bug | Status: closed

Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Josh Smeaton <josh.smeaton@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"69c6a6868f0b4137bb293ff4326ecf4681506c37"]:
{{{
#!CommitTicketReference repository=""
revision="69c6a6868f0b4137bb293ff4326ecf4681506c37"
Fixed #24174 -- Fixed extra order by descending
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24174#comment:5>

Django

unread,
Jan 18, 2015, 9:18:50 PM1/18/15
to django-...@googlegroups.com
#24174: Extra order by ignores descending orderings
-------------------------------------+-------------------------------------
Reporter: BertrandBordage | Owner: jarshwah
Type: Bug | Status: closed
Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Josh Smeaton <josh.smeaton@…>):

In [changeset:"0c910823c166b827b090df5c5c8be9e502a14d8c"]:
{{{
#!CommitTicketReference repository=""
revision="0c910823c166b827b090df5c5c8be9e502a14d8c"
[1.8.x] Fixed #24174 -- Fixed extra order by descending

Backport of 69c6a6868f0b4137bb293ff4326ecf4681506c37 from master
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/24174#comment:6>

Reply all
Reply to author
Forward
0 new messages