Opened 7 years ago
Closed 7 years ago
#30242 closed Cleanup/optimization (fixed)
Double spaces before limit/offset clause in as_sql() of SQLCompiler
| Reported by: | Hang Park | Owned by: | Hang Park |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | dev |
| Severity: | Normal | Keywords: | sql databaseoperations sqlcompiler |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | yes | UI/UX: | no |
Description
SQLCompiler has as_sql() method to create the SQL for a given query. Specifically, end-users can get the SQL string of their QuerySet by this method like:
User.objects.values('id').query.__str__()
where User is a basic user model.
Absolutely, this returns a SQL string SELECT "auth_user"."id" FROM "auth_user", and note that every separator between tokens is all single space.
However, any query which has limit/offset clause produces double spaces before limit/offset clause. For example:
User.objects.values('id')[1:2].query.__str__()
returns
SELECT "auth_user"."id" FROM "auth_user" LIMIT 1 OFFSET 1
not
SELECT "auth_user"."id" FROM "auth_user" LIMIT 1 OFFSET 1
These queries are executed as well as expected but seem not a good practice for formatting SQL as a string.
Change History (4)
comment:1 by , 7 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:2 by , 7 years ago
| Has patch: | set |
|---|
comment:3 by , 7 years ago
| Triage Stage: | Unreviewed → Ready for checkin |
|---|
OK, given that you've patched this striaght-away, very happy to accept. Looks good.
(Welcome aboard!)
PR open at https://github.com/django/django/pull/11061