Opened 16 years ago
Closed 16 years ago
#12814 closed (duplicate)
Missing ORDER BY in subselect
| Reported by: | EsOsO | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | 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
Python 2.6.4 (r264:75706, Dec 7 2009, 18:45:15)
>>> from django.db import connection
>>> from pokertour.models import Session, Entry
>>> s = Session.objects.all()[:4]
>>> s
[<Session: Session 033>, <Session: Session 032>, <Session: Session 031>, <Session: Session 030>]
>>> e = Entry.objects.filter(session__in=s)
>>> e
[<Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, <Entry: Entry object>, '...(remaining elements truncated)...']
>>> connection.queries
[{'time': '0.005', 'sql': 'SELECT "pokertour_session"."id", "pokertour_session"."name", "pokertour_session"."slug", "pokertour_session"."date", "pokertour_session"."location_id" FROM "pokertour_session" ORDER BY "pokertour_session"."date" DESC LIMIT 4'}, {'time': '0.004', 'sql': 'SELECT "pokertour_entry"."id", "pokertour_entry"."player_id", "pokertour_entry"."session_id", "pokertour_entry"."cash_in", "pokertour_entry"."cash_out", "pokertour_entry"."bank_in", "pokertour_entry"."stack" FROM "pokertour_entry" WHERE "pokertour_entry"."session_id" IN (SELECT U0."id" FROM "pokertour_session" U0 LIMIT 4) LIMIT 21'}]
>>>
The subquery should return the same records as the first, instead it loses the order by clause:
...FROM "pokertour_session" ORDER BY "pokertour_session"."date" DESC LIMIT 4
...FROM "pokertour_session" U0 LIMIT 4
Note:
See TracTickets
for help on using tickets.
This is effectively the same problem as #12328.