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: Cheng Zhang <czhang.cmu+web@…> 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)

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

Download all attachments as: .zip

Change History (6)

by Cheng Zhang <czhang.cmu+web@…>, 19 years ago

Attachment: 1730.patch added

comment:1 by Adrian Holovaty, 19 years ago

Status: newassigned

comment:2 by Adrian Holovaty, 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 Adrian Holovaty, 19 years ago

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

comment:4 by Chris Rose, 19 years ago

milestone: Version 0.91
Version: magic-removalSVN

comment:5 by Russell Keith-Magee, 18 years ago

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