Opened 5 years ago
Closed 5 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)
Change History (2)
by , 5 years ago
comment:1 by , 5 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
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.
ORM Query and SQL generated Query