Code

Opened 4 years ago

Closed 13 months ago

#13198 closed Bug (fixed)

exclude() bug when used with Q() objects

Reported by: mucisland Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1
Severity: Normal Keywords: exclude Q
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

.exclude() behaves different when its argument is wrapped into Q(). Specifically, when wrapping an exclude filter with Q(), objects containing NULL in related fields are excluded as well as objects which have matching content in this field.

The SQL which is generated by exclude(<field>=<value>) should _not_ match objects with NULL in the respective field, but this is sometimes violated when wrapping the filter with Q(). (So far I've only seen this bug when using a filter which specifies a field in a related object.)

The attached file contains a minimum Django project (project name is "sandbox", application name is "q") with two test cases which should clarify the described behaviour.

Best Regards,
Dirk

Attachments (1)

sandbox.zip (6.3 KB) - added by mucisland 4 years ago.
Django project "sandbox" with app "q" which reproduces the bug. Execute "manage.py test".

Download all attachments as: .zip

Change History (6)

Changed 4 years ago by mucisland

Django project "sandbox" with app "q" which reproduces the bug. Execute "manage.py test".

comment:1 Changed 4 years ago by russellm

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

I'm fairly certain that this is a duplicate of #11052; one of the examples in the comments seems to match up.

comment:2 Changed 14 months ago by jedediah@…

  • Easy pickings unset
  • Severity set to Normal
  • Type set to Uncategorized
  • UI/UX unset

Still seeing this in Django 1.4, so either it was erroneously marked as a dupe or it snuck back in. It's pretty simple to reproduce:

MyModel.objects.exclude(nullable_field=123)   => includes rows with nullable_field=null
MyModel.objects.exclude(Q(nullable_field=123))   => excludes rows with nullable_field=null

comment:3 Changed 14 months ago by akaariai

  • Resolution duplicate deleted
  • Status changed from closed to new
  • Triage Stage changed from Unreviewed to Accepted

I tried this with master, one of the tests pass, the other doesn't. After the patch to #17600 this one would be fixed this.

I'm reopening this one so that proper test will be added in #17600.

comment:4 Changed 14 months ago by akaariai

  • Type changed from Uncategorized to Bug

comment:5 Changed 13 months ago by akaariai

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

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.