Opened 6 years ago
Closed 6 years ago
#29476 closed Bug (invalid)
Filter with BooleanField produces invalid SQL for .query
Reported by: | Martin Gropp | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.11 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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
As discussed in #17741, the
query
attribute isn't intended to give valid SQL.