Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#7957 closed (duplicate)

Chaining Queryset.extra() methods that contain parameters breaks the query

Reported by: mullendr Owned by: mtredinnick
Component: Database layer (models, ORM) Version: master
Severity: Keywords: queryset extra select select_params
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by mtredinnick)

To reproduce:

Precondition a user 'root' exists with id=1

from django.contrib.auth.models import User
for result in User.objects.extra(select=
        {'foo3': "SELECT COUNT(auth_user.id) FROM auth_user WHERE (auth_user.username = %s)"}
        , select_params=['root']).extra(select=
        {'foo2': "SELECT COUNT(auth_user.id) FROM auth_user WHERE (auth_user.id = %s)"}
        , select_params=['1']):
    print result.foo3
# output should be:
# 1
# 1
for result in User.objects.extra(select=
        {'foo1': "SELECT COUNT(auth_user.id) FROM auth_user WHERE (auth_user.username = %s)"}
        , select_params=['root']).extra(select=
        {'foo2': "SELECT COUNT(auth_user.id) FROM auth_user WHERE (auth_user.id = %s)"}
        , select_params=['1']):
    print result.foo1
# output should be:
# 1
# 1

Attachments (2)

django_extra.diff (1.3 KB) - added by mullendr 6 years ago.
patch to fix extra().extra() problem
reproduce_extra_problem.py (801 bytes) - added by mullendr 6 years ago.
file that demonstrated bug

Download all attachments as: .zip

Change History (10)

Changed 6 years ago by mullendr

patch to fix extra().extra() problem

Changed 6 years ago by mullendr

file that demonstrated bug

comment:1 Changed 6 years ago by mullendr

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to fixed
  • Status changed from new to closed

comment:2 Changed 6 years ago by mullendr

  • Resolution fixed deleted
  • Status changed from closed to reopened

comment:3 follow-up: Changed 6 years ago by mtredinnick

  • Description modified (diff)
  • milestone changed from 1.0 beta to 1.0
  • Triage Stage changed from Unreviewed to Accepted

The patch looks like it's along the right lines, although I don't really like adding an append() method to the sorted dictionary. That's a bit of API creep that we can probably avoid.

Fixed milestone and description.

comment:4 in reply to: ↑ 3 Changed 6 years ago by mullendr

Replying to mtredinnick:

The patch looks like it's along the right lines, although I don't really like adding an append() method to the sorted dictionary. That's a bit of API creep that we can probably avoid.


I see the SortedDict more as a list with an additional dict interface. But I am curious how you would solve it.

Also if you are going to work on this ticket you might want to check out ticket #7961 as well since it reports and solves an additional bug in this area of the code.

comment:5 Changed 6 years ago by mtredinnick

  • Owner changed from mullendr to mtredinnick
  • Status changed from reopened to new

comment:6 Changed 6 years ago by mtredinnick

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

Closing as a dupe of #8191, which is for the broader problem. This ticket is a symptom of the larger problem with extra's parameter handling. Fix isn't hard, but I can't do it in isolation from #7961.

comment:7 Changed 6 years ago by mtredinnick

(In [8426]) Changed the (internal) way extra(select=.., select_params=...) handling is done
so that parameters stay with their select items. This means that merging and
trimming of those items is handled correctly.

Refs #7957, #7961. Fixed #8191.

comment:6 Changed 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.