Opened 11 years ago

Closed 10 years ago

#1730 closed defect (duplicate)

[patch] ORM shouldn't add table prefix for a custom SELECT column

Reported by: Cheng Zhang <czhang.cmu+web@…> Owned by: Adrian Holovaty
Component: Database layer (models, ORM) Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

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)

1730.patch (1.7 KB) - added by Cheng Zhang <czhang.cmu+web@…> 11 years ago.

Download all attachments as: .zip

Change History (6)

Changed 11 years ago by Cheng Zhang <czhang.cmu+web@…>

Attachment: 1730.patch added

comment:1 Changed 11 years ago by Adrian Holovaty

Status: newassigned

comment:2 Changed 11 years ago by Adrian Holovaty

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 Changed 11 years ago by Adrian Holovaty

Hmm, this is an undoing of [2803], which was done to fix ordering with a custom select. Can we solve both problems?

comment:4 Changed 10 years ago by Chris Rose

milestone: Version 0.91
Version: magic-removalSVN

comment:5 Changed 10 years ago by Russell Keith-Magee

Resolution: duplicate
Status: assignedclosed

AFAICT, this is a duplicate of #1685, which has the same patch as [2803], not a reversal.

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