Last active
January 1, 2016 16:49
-
-
Save pipermerriam/8173360 to your computer and use it in GitHub Desktop.
Django Bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.db import models | |
| class Owner(models.Model): | |
| pass | |
| class Thing(models.Model): | |
| owner = models.ForeignKey(Owner, related_name='things') | |
| date = models.DateField() | |
| class Error(models.Model): | |
| owner = models.ForeignKey(Owner, related_name='errors') | |
| date = models.DateField() | |
| from django.db.models import F | |
| # This works | |
| Error.objects.filter(owner__things__date=F('date')) | |
| # | |
| ## This Doesnt | |
| Error.objects.exclude(owner__things__date=F('date')) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Django 1.6.1 | |
| # In [7]: print Error.objects.exclude(owner__things__date=F('date')).query | |
| SELECT `arst_error`.`id`, `arst_error`.`owner_id`, `arst_error`.`date` FROM `arst_error` WHERE NOT (`arst_error`.`owner_id` IN (SELECT U2.`owner_id` FROM `arst_error` U0 INNER JOIN `arst_thing` U2 ON ( U1.`id` = U2.`owner_id` ) WHERE U2.`date` = U0.`date`)) | |
| # In [8]: print Error.objects.filter(owner__things__date=F('date')).query | |
| SELECT `arst_error`.`id`, `arst_error`.`owner_id`, `arst_error`.`date` FROM `arst_error` INNER JOIN `arst_owner` ON ( `arst_error`.`owner_id` = `arst_owner`.`id` ) INNER JOIN `arst_thing` ON ( `arst_owner`.`id` = `arst_thing`.`owner_id` ) WHERE `arst_thing`.`date` = `arst_error`.`date` | |
| # Django 1.7. | |
| #>>> print Error.objects.exclude(owner__things__date=F('date')).query | |
| SELECT "arst_error"."id", "arst_error"."owner_id", "arst_error"."date" FROM "arst_error" WHERE NOT ("arst_error"."owner_id" IN (SELECT U2."owner_id" FROM "arst_error" U0 INNER JOIN "arst_thing" U2 ON ( U1."id" = U2."owner_id" ) WHERE U2."date" = U0."date")) | |
| #>>> print Error.objects.filter(owner__things__date=F('date')).query | |
| SELECT "arst_error"."id", "arst_error"."owner_id", "arst_error"."date" FROM "arst_error" INNER JOIN "arst_owner" ON ( "arst_error"."owner_id" = "arst_owner"."id" ) INNER JOIN "arst_thing" ON ( "arst_owner"."id" = "arst_thing"."owner_id" ) WHERE "arst_thing"."date" = "arst_error"."date" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment