Opened 9 years ago

Closed 9 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
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@…> 9 years ago.

Download all attachments as: .zip

Change History (6)

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

comment:1 Changed 9 years ago by adrian

  • Status changed from new to assigned

comment:2 Changed 9 years ago by adrian

  • Summary changed from ORM shouldn't add table prefix for a custom SELECT column to [patch] ORM shouldn't add table prefix for a custom SELECT column

comment:3 Changed 9 years ago by adrian

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 9 years ago by Chris Rose

  • milestone Version 0.91 deleted
  • Version changed from magic-removal to SVN

comment:5 Changed 9 years ago by russellm

  • Resolution set to duplicate
  • Status changed from assigned to closed

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