Code

Opened 4 years ago

Closed 4 years ago

Last modified 3 years ago

#13438 closed (duplicate)

bug with isnull ForeignKey queries?

Reported by: wkornewald Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I'm not sure if this really is a bug. I have these models:

from django.db import models

class A(models.Model):
    number = models.IntegerField(null=True)

class X(models.Model):
    fk = models.ForeignKey(A, null=True)

And when I run this query:

X.objects.filter(fk=None)[:10]

The generated SQL is this:

SELECT "djangoappengine_x"."id", "djangoappengine_x"."fk_id"
FROM "djangoappengine_x"
LEFT OUTER JOIN "djangoappengine_a"
  ON ("djangoappengine_x"."fk_id" = "djangoappengine_a"."id")
WHERE "djangoappengine_a"."id" IS NULL
LIMIT 10

Shouldn't the generated query be this:

SELECT "djangoappengine_x"."id", "djangoappengine_x"."fk_id"
FROM "djangoappengine_x"
WHERE "djangoappengine_x"."fk_id" IS NULL
LIMIT 10

Is there a reason why the query uses a LEFT OUTER JOIN on model A in this case (and the JOIN doesn't get trimmed) or is this just an incomplete edge case where the actual intention is to handle queries like X.objects.filter(fk__number=None)?

Attachments (0)

Change History (2)

comment:1 Changed 4 years ago by Alex

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

Dupe of #10790.

comment:2 Changed 3 years ago by jacob

  • milestone 1.2 deleted

Milestone 1.2 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.