Opened 16 years ago
Closed 12 years ago
#12807 closed Bug (fixed)
Empty results break when part of ORs
| Reported by: | Samuel Cormier-Iijima | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.1 |
| Severity: | Normal | Keywords: | query, sql, q, filter, or, empty |
| Cc: | sciyoshi@… | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | yes |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
The following query:
User.objects.filter((Q(pk=3) | ~Q(pk__in=[]) & Q(pk=1))
currently breaks, since ~Q(pk__in=[]) isn't noticed as being full, thus leading to the following query:
User.objects.filter((Q(pk=3) | ~Q(pk__in=[])) & Q(pk=1)).values('id').query.as_sql()
('SELECT "auth_user"."id" FROM "auth_user" WHERE (("auth_user"."id" = %s ) AND "auth_user"."id" = %s )', (3, 1))
and giving an empty result set, instead of the correct
User.objects.filter((Q(pk=3) | ~Q(pk__in=[])) & Q(pk=1)).values('id').query.as_sql()
('SELECT "auth_user"."id" FROM "auth_user" WHERE ("auth_user"."id" = %s )', (1,))
which returns the single user with PK 1.
Attaching a patch which fixes it by making as_sql() raise FullResultSet and adds a test case. The issue could also be fixed by checking if the child's as_sql() is empty, but this seemed cleaner.
Attachments (1)
Change History (9)
by , 16 years ago
| Attachment: | 12807.patch added |
|---|
comment:1 by , 16 years ago
comment:2 by , 16 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 15 years ago
| Type: | → Bug |
|---|
comment:4 by , 15 years ago
| Severity: | → Normal |
|---|
comment:5 by , 15 years ago
| Easy pickings: | unset |
|---|---|
| Patch needs improvement: | set |
12807.patch fails to apply cleanly on to trunk
comment:7 by , 12 years ago
| Description: | modified (diff) |
|---|
comment:8 by , 12 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
Sorry about the formatting (should probably use preview next time). That should have read:
The following query:
currently breaks, since ~Q(pkin=[]) isn't noticed as being full, thus leading to the following query:
>>> User.objects.filter((Q(pk=3) | ~Q(pkin=[])) & Q(pk=1)).values('id').query.as_sql() ('SELECT "auth_user"."id" FROM "auth_user" WHERE (("auth_user"."id" = %s ) AND "auth_user"."id" = %s )', (3, 1))and giving an empty result set, instead of the correct
>>> User.objects.filter((Q(pk=3) | ~Q(pkin=[])) & Q(pk=1)).values('id').query.as_sql() ('SELECT "auth_user"."id" FROM "auth_user" WHERE ("auth_user"."id" = %s )', (1,))which returns the single user with PK 1.