Opened 19 years ago
Closed 18 years ago
#1730 closed defect (duplicate)
[patch] ORM shouldn't add table prefix for a custom SELECT column
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
For code like this:
from mysite.polls.models import Poll, Choice from django.db import connection select = { 'choices': 'SELECT COUNT(*) FROM polls_choice WHERE poll_id=polls_poll.id', } p = Poll.objects.extra(select=select).order_by('choices') try: print "p=", p except: pass print "db.queries=%s" % connection.queries
The ORM generates SQL statement as:
SELECT "polls_poll"."id","polls_poll"."question","polls_poll"."pub_date", (SELECT COUNT(*) FROM polls_choice WHERE poll_id=polls_poll.id) AS "choices" FROM "polls_poll" ORDER BY "polls_poll"."choices" ASC
That's incorrect. The correct statement should be:
SELECT "polls_poll"."id","polls_poll"."question","polls_poll"."pub_date", (SELECT COUNT(*) FROM polls_choice WHERE poll_id=polls_poll.id) AS "choices" FROM "polls_poll" ORDER BY "choices" ASC
A patch will follow.
Attachments (1)
Change History (6)
by , 19 years ago
Attachment: | 1730.patch added |
---|
comment:1 by , 19 years ago
Status: | new → assigned |
---|
comment:2 by , 19 years ago
Summary: | ORM shouldn't add table prefix for a custom SELECT column → [patch] ORM shouldn't add table prefix for a custom SELECT column |
---|
comment:3 by , 19 years ago
comment:4 by , 19 years ago
milestone: | Version 0.91 |
---|---|
Version: | magic-removal → SVN |
comment:5 by , 18 years ago
Resolution: | → duplicate |
---|---|
Status: | assigned → closed |
Note:
See TracTickets
for help on using tickets.
Hmm, this is an undoing of [2803], which was done to fix ordering with a custom
select
. Can we solve both problems?