[Django] #32476: Django Query Generating Style Error

6 views
Skip to first unread message

Django

unread,
Feb 23, 2021, 5:52:51 AM2/23/21
to django-...@googlegroups.com
#32476: Django Query Generating Style Error
-------------------------------------+-------------------------------------
Reporter: thajones | Owner: nobody
Type: Bug | Status: new
Component: Database | Version: 3.1
layer (models, ORM) | Keywords: Query,
Severity: Normal | Missing_Brackets
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
var = modelname.objects.exclude( field1__isnull = True, field1__exact = ''
)

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/32476>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 23, 2021, 5:54:56 AM2/23/21
to django-...@googlegroups.com
#32476: Django Query Generating Style Error
-------------------------------------+-------------------------------------
Reporter: thajones | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: Query, | Triage Stage:
Missing_Brackets | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by thajones):

* Attachment "Buggy.png" added.

ORM Query and SQL generated Query

Django

unread,
Feb 23, 2021, 7:03:49 AM2/23/21
to django-...@googlegroups.com
#32476: Django Query Generating Style Error
-------------------------------------+-------------------------------------
Reporter: thajones | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 3.1
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: Query, | Triage Stage:
Missing_Brackets | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/32476#comment:1>

Reply all
Reply to author
Forward
0 new messages