[Django] #29476: Filter with BooleanField produces invalid SQL for .query

4 views
Skip to first unread message

Django

unread,
Jun 6, 2018, 11:43:47 AM6/6/18
to django-...@googlegroups.com
#29476: Filter with BooleanField produces invalid SQL for .query
-------------------------------------+-------------------------------------
Reporter: Martin | Owner: nobody
Gropp |
Type: Bug | Status: new
Component: Database | Version: 1.11
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Hiho!

I ran into this issue today on 1.11.13:
I am using `.filter` on a QuerySet to select all elements that have a
`BooleanField` set to true.
This works fine when run against the database, but the SQL produced by
`QuerySet.query` is incorrect.

Here's an example:
{{{
class Foo(Model):
bar = BooleanField()

query = str(Foo.objects.filter(bar=True).query)
}}}

Will produce SQL like this:
{{{
SELECT "table"."id", "table"."bar" FROM "table" WHERE "table"."bar" = True
}}}

This will not work, because `True` is interpreted as a column name.

(I was trying to use this SQL as part of a raw query, and ended up working
around the problem by replacing the `.filter` with another raw query
casting `bar` to integer in the `where`.)


Cheers
Martin

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

Django

unread,
Jun 6, 2018, 12:12:57 PM6/6/18
to django-...@googlegroups.com
#29476: Filter with BooleanField produces invalid SQL for .query
-------------------------------------+-------------------------------------
Reporter: Martin Gropp | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.11
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

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

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


Comment:

As discussed in #17741, the `query` attribute isn't intended to give valid
SQL.

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

Reply all
Reply to author
Forward
0 new messages