Opened 5 years ago

Closed 4 years ago

#13579 closed Bug (duplicate)

None gets ignored by __in filter

Reported by: outofculture Owned by: gruszczy
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX:

Description

from django.db import models

class A(models.Model):
  b = models.ForeignKey(B, null=True)

class B(models.Model):
  pass

for _ in range(0,10):
  A.objects.create()

b = B.objects.create()
A.objects.create(b=b)

A.objects.filter(b=None).count()  # => 10
A.objects.filter(b__in=[None,]).count() # => 0 (should be 10)
A.objects.filter(b__in=[None,b]).count() # => 1 (should be 11)
A.objects.filter(b__in=[b,None]).count() # => 1 (should be 11)

I haven't tested with the above code exactly, but this is the failing behavior I'm seeing.

Attachments (1)

13579.patch (1.9 KB) - added by gruszczy 5 years ago.
patch + simple test

Download all attachments as: .zip

Change History (7)

comment:1 Changed 5 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

This is an edge case caused by the fact that x = NULL and x IS NULL are not equivalent in SQL. The filter clause "bin=[None, value]" needs to be rolled out as "b IS NULL OR b IN (value)"

Changed 5 years ago by gruszczy

patch + simple test

comment:2 Changed 5 years ago by gruszczy

  • Has patch set

I don't know, whether this is a way to go, so I would appreciate any advice on the patch and would be happy to work more on it, if required.

comment:3 Changed 5 years ago by gruszczy

  • Owner changed from nobody to gruszczy

comment:4 Changed 4 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:5 Changed 4 years ago by dmclain

  • Easy pickings unset
  • Patch needs improvement set

Looks like the implementation for __in has changed since the patch was made. This is more than just the patch not applying cleanly, there are more code paths that need to be considered now.

comment:6 Changed 4 years ago by jacob

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

Duplicate of #13768.

Note: See TracTickets for help on using tickets.
Back to Top