Opened 3 years ago

Closed 3 years ago

#32476 closed Bug (invalid)

Django Query Generating Style Error

Reported by: thajones Owned by: nobody
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords: Query, Missing_Brackets
Cc: jones.thayil@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

var = modelname.objects.exclude( field1isnull = True, field1exact = )

print(var.query)
# select * from modelname where not ( field1 != and field1 is not null and field1 is null ) which is always a empty set.

Expected Query:
# select * from modelname where not ( field1 != and field1 is null )
OR
# select * from modelname where not ((field1 !=
and field1 is not null) and field1 is null )

which gives resultant set with non empty and not null records.

Attachments (1)

Buggy.png (31.9 KB ) - added by thajones 3 years ago.
ORM Query and SQL generated Query

Download all attachments as: .zip

Change History (2)

by thajones, 3 years ago

Attachment: Buggy.png added

ORM Query and SQL generated Query

comment:1 by Mariusz Felisiak, 3 years ago

Resolution: invalid
Status: newclosed

That's not an issue in the ORM but in your filters, you described the set of rows that is always empty: field1 IS NULL and field1 = '' (which implies that field1 IS NOT NULL). Also, wrapping conditions with the same logical operator doesn't change anything, because

NOT (field1 != '' AND field1 IS NOT NULL and field1 IS NULL)

is equivalent to

NOT ((field1 != '' AND field1 IS NOT NULL) and field1 IS NULL)

You should use .exclude(Q(field1__isnull=True) | Q(field1='')).

Please don't use Trac as a support channel in the future. Closing per TicketClosingReasons/UseSupportChannels.

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