Django

Code

Ticket #3141 (closed: fixed)

Opened 1 year ago

Last modified 3 weeks ago

can't use count() on querysets when extra() has been used with params.

Reported by: mrmachine <real dot human at mrmachine dot net> Assigned to: deepak
Component: Database wrapper Version: SVN
Keywords: queryset extra params count, qs-rf-fixed Cc: simon@quo.com.au, real.human@mrmachine.net, heckj@mac.com
Triage Stage: Accepted Has patch: 1
Needs documentation: 0 Needs tests: 1
Patch needs improvement: 0

Description

there seems to be a bug in django/db/models/query.py when using extra() with params on a queryset. the queryset is generated correctly, i can access it, iterate through it, but i can't count() it.

>>> from django.contrib.auth.models import User
>>> u = User.objects.filter(pk=1).extra(select={'foo': '%s'}, params=['1'])
>>> u[0].foo
'1'
>>> u.count()
Traceback (most recent call last):
  File "<console>", line 1, in ?
  File "/Volumes/Singularity/Projects/django/django/db/models/query.py", line 202, in count
    cursor.execute("SELECT COUNT(*)" + sql, params)
  File "/Volumes/Singularity/Projects/django/django/db/backends/util.py", line 19, in execute
    self.db.queries.append({
TypeError: not all arguments converted during string formatting
>>> u = User.objects.filter(pk=1).extra(select={'foo': '1'})
>>> u[0].foo
'1'
>>> u.count()
1

Attachments

extra_param_fix.diff (0.6 kB) - added by deepak <deep.thukral@gmail.com> on 09/14/07 02:30:32.

Change History

01/19/07 01:08:39 changed by heckj@mac.com

  • cc changed from real.human@mrmachine.net to real.human@mrmachine.net, heckj@mac.com.

I just ran into a similiar situation this evening with the queryset not putting the arguments in the correct ordering, specifically when tacking on two "extra" sets to an otherwise kind of plain jane queryset. I'm trying to work it down to an easily reproducible case or example. In my case, it also appears to be related to using .count() at the tail end of the queryset chain.

If I pull back the queryset, force it into a list, then I can count it - but that sort of defeats the purpose of being able to use the count.

Ah - I narrowed it down to when it's using an extra(select=...) statement. In django/db/models/query.py around line 202, it's invoking: cursor.execute("SELECT COUNT(*)" + sql, params)

The additional "select" param is added to the list, but the additional "select" component isn't, resulting in the "not all arguments converted" error.

Not sure how to nessecarily resolve this though.

02/17/07 10:37:06 changed by Simon G. <dev@simon.net.nz>

  • stage changed from Unreviewed to Accepted.

09/13/07 16:38:14 changed by mtredinnick

  • keywords changed from queryset extra params count to queryset extra params count, qs-rf-fixed.

09/14/07 02:30:32 changed by deepak <deep.thukral@gmail.com>

  • attachment extra_param_fix.diff added.

09/14/07 02:31:27 changed by deepak <deep.thukral@gmail.com>

  • owner changed from nobody to deepak.
  • has_patch set to 1.
  • version set to SVN.
  • needs_tests set to 1.

Main problem is that while logging sql, we are not handling extras in sql queries. I fixed this issue, but needs test.

03/20/08 04:13:28 changed by amit.ramon@gmail.com

The problem still exists with the latest svn version. It seems to a problem with using select with parameters in extra (extra where with parameters is working fine).

03/20/08 14:16:04 changed by mtredinnick

(In [7340]) queryset-refactor: Fixed up extra(select=...) calls with parameters so that the parameters are substituted in correctly in all cases. This introduces an extra argument to extra() for this purpose; no alternative there.

Also fixed values() to work if you don't specify *all* the extra select aliases in the values() call.

Refs #3141.

04/16/08 20:52:19 changed by Simon Litchfield <simon@quo.com.au>

  • cc changed from real.human@mrmachine.net, heckj@mac.com to simon@quo.com.au, real.human@mrmachine.net, heckj@mac.com.

04/26/08 21:50:16 changed by mtredinnick

  • status changed from new to closed.
  • resolution set to fixed.

(In [7477]) Merged the queryset-refactor branch into trunk.

This is a big internal change, but mostly backwards compatible with existing code. Also adds a couple of new features.

Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658


Add/Change #3141 (can't use count() on querysets when extra() has been used with params.)




Change Properties
Action