Code

Opened 7 years ago

Closed 6 years ago

#3037 closed defect (fixed)

Q(a=1, b=2) treated as Q(a=1) | Q(b=2)

Reported by: Andrew McNabb <amcnabb@…> Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: normal Keywords: qs-rf-fixed
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

As far as I can tell from the documentation, arguments to a Q are anded together, e.g. Q(a=1, b=2) is equivalent to Q(a=1) & Q(b=2). However, I am currently finding (in revision 4085) that Q(a=1, b=2) is instead equivalent to Q(a=1) | Q(b=2). In this particular case, I am doing something like:

q1 = Q(a=1, b=2)

q2 = Q(a=3, b=4)

model.objects.filter(q1 | q2)

When I look at connection.queries, I find that there is no parenthesization at all, and all of the terms are combined with OR like "a=1 OR b=2 OR a=3 OR b=4" instead of "(a=1 AND b=2) OR (a=3 AND b=4)".

I made my description a little bit abstract for clarity, but I can post the exact queries if necessary. Right now I'm working around the problem by doing something of the form:

q1 = Q(a=1) & Q(b=2)

etc., which gives the query I expected. I hope this report is clear. Let me know if you have any questions.

Attachments (0)

Change History (6)

comment:1 Changed 7 years ago by Simon G. <dev@…>

  • Triage Stage changed from Unreviewed to Design decision needed

comment:2 Changed 7 years ago by mtredinnick

  • Owner changed from adrian to mtredinnick
  • Triage Stage changed from Design decision needed to Accepted

Yeah, this is broken. It will be fixed post-0.96 (but pre-1.0), since fixing all the problems with disjunctive and conjunctive queries (as well as join types, etc) requires a large refactoring of the QuerySet class. I'm working on this area, so I'll take this one.

comment:3 Changed 7 years ago by ikelly

  • Keywords qs-rf added

comment:4 Changed 7 years ago by mtredinnick

(In [6514]) queryset-refactor: Added a test to show that #3037 is really fixed. Refs #3037.

comment:5 Changed 7 years ago by mtredinnick

  • Keywords qs-rf-fixed added; qs-rf removed

comment:6 Changed 6 years ago by mtredinnick

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

(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 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.