Opened 15 years ago
Last modified 13 years ago
#12870 closed
ORM bug with using exclude in conjunction with Q objects — at Initial Version
Reported by: | Yeago | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.1 |
Severity: | 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
Here's a shell session which illustrates the problem.
from tapped.deck.models import Deck
from django.db.models import Q
Deck.objects.filter(userusername='nammertime')
[<Deck: Walled Out (Legacy) *Private*>, <Deck: Reign of Fire (Legacy) *Private*>, <Deck: Wake of the Cataclysm (Legacy) *Private*>, <Deck: WMDs (Extended) *Private*>, <Deck: Chernabog (Legacy) *Private*>, <Deck: Dark Alliance (Legacy) *Private*>, <Deck: Elf Horde (Legacy) *Private*>, <Deck: Natural Selection *Private*>, <Deck: Sliver Rush (Legacy) *Private*>, <Deck: Rath's Last Conscripts (Legacy) *Private*>, <Deck: Furious Assault (Legacy) *Private*>, <Deck: Urza's Venom (Legacy) *Private*>, <Deck: Sapling EDH (Legacy) *Private*>, <Deck: Ashling EDH (Legacy) *Private*>]
Deck.objects.filter(userusername='nammertime').count()
14
Deck.objects.filter(userusername='nammertime').exclude(category='pro').exclude(category='precon')
[<Deck: Walled Out (Legacy) *Private*>, <Deck: Reign of Fire (Legacy) *Private*>, <Deck: Wake of the Cataclysm (Legacy) *Private*>, <Deck: WMDs (Extended) *Private*>, <Deck: Chernabog (Legacy) *Private*>, <Deck: Dark Alliance (Legacy) *Private*>, <Deck: Elf Horde (Legacy) *Private*>, <Deck: Natural Selection *Private*>, <Deck: Sliver Rush (Legacy) *Private*>, <Deck: Rath's Last Conscripts (Legacy) *Private*>, <Deck: Furious Assault (Legacy) *Private*>, <Deck: Urza's Venom (Legacy) *Private*>, <Deck: Sapling EDH (Legacy) *Private*>, <Deck: Ashling EDH (Legacy) *Private*>]
Deck.objects.filter(userusername='nammertime').exclude(category='pro').exclude(category='precon').count()
14
Deck.objects.filter(userusername='nammertime').exclude(Q(category='pro')|Q(category='precon'))
Deck.objects.filter(userusername='nammertime').exclude(Q(category='pro')|Q(category='precon'))
[]
Deck.objects.filter(userusername='nammertime').exclude(Q(category='pro')|Q(category='precon')).count()
0
Deck.objects.filter(userusername='nammertime').filter(category='pro')
[]
Deck.objects.filter(userusername='nammertime').filter(category='precon')
[]
Deck.objects.filter(userusername='nammertime').exclude(Q(category='pro')|Q(category='precon')).query.as_sql()
('SELECT deck_deck
.id
, deck_deck
.format_id
, deck_deck
.name
, deck_deck
.slug
, deck_deck
.user_id
, deck_deck
.description
, deck_deck
.profile_cardinality
, deck_deck
.date_added
, deck_deck
.date_updated
, deck_deck
.date_refreshed
, deck_deck
.is_private
, deck_deck
.category
, deck_deck
.preconstructed_set_id
, deck_deck
.cost
, deck_deck
.view_count_cache
, deck_deck
.comment_count_cache
, deck_deck
.data_generated
, deck_deck
.image_token
, deck_deck
.similar_due
, deck_deck
.rating_votes
, deck_deck
.rating_score
FROM deck_deck
INNER JOIN auth_user
ON (deck_deck
.user_id
= auth_user
.id
) WHERE (auth_user
.username
= %s AND NOT ((deck_deck
.category
= %s OR deck_deck
.category
= %s )))', ('nammertime', 'pro', 'precon'))
Deck.objects.filter(userusername='nammertime').exclude(category='pro').exclude(category='precon').query.as_sql()
('SELECT deck_deck
.id
, deck_deck
.format_id
, deck_deck
.name
, deck_deck
.slug
, deck_deck
.user_id
, deck_deck
.description
, deck_deck
.profile_cardinality
, deck_deck
.date_added
, deck_deck
.date_updated
, deck_deck
.date_refreshed
, deck_deck
.is_private
, deck_deck
.category
, deck_deck
.preconstructed_set_id
, deck_deck
.cost
, deck_deck
.view_count_cache
, deck_deck
.comment_count_cache
, deck_deck
.data_generated
, deck_deck
.image_token
, deck_deck
.similar_due
, deck_deck
.rating_votes
, deck_deck
.rating_score
FROM deck_deck
INNER JOIN auth_user
ON (deck_deck
.user_id
= auth_user
.id
) WHERE (auth_user
.username
= %s AND NOT (deck_deck
.category
= %s AND NOT (deck_deck
.category
IS NULL)) AND NOT (deck_deck
.category
= %s AND NOT (deck_deck
.category
IS NULL)))', ('nammertime', 'pro', 'precon'))