Opened 5 years ago

Closed 5 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 Hang Park, 5 years ago

Owner: changed from nobody to Hang Park
Status: newassigned

comment:2 by Hang Park, 5 years ago

Has patch: set

comment:3 by Carlton Gibson, 5 years ago

Triage Stage: UnreviewedReady for checkin

OK, given that you've patched this striaght-away, very happy to accept. Looks good.
(Welcome aboard!)

comment:4 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In 142e1ead:

Fixed #30242 -- Removed extra space before LIMIT/OFFSET SQL.

Note: See TracTickets for help on using tickets.
Back to Top