﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
33435	Subquery.as_sql() generates invalid SQL.	M1ha Shvn	Mohamed Nabil Rady	"Since [https://github.com/django/django/commit/3a505c70e7b228bf1212c067a8f38271ca86ce09 this commit] `Subquery.as_sql(...)` method returns incorrect SQL removing first and last symbols instead of absent breakets. Adding `Subquery().query.subquery = True` attribute fixes the problem. From my point of view, it should be set in `Subquery` constructor.

{{{
from django.db import connection
from apps.models import App

q = Subquery(App.objects.all())

print(str(q.query))

# Output SQL is valid:
#  'SELECT ""apps_app"".""id"", ""apps_app"".""name"" FROM ""apps_app""'

print(q.as_sql(q.query.get_compiler('default'), connection))

# Outptut SQL is invalid (no S letter at the beggining and "" symbol at the end):
# ('(ELECT ""apps_app"".""id"", ""apps_app"".""name"" FROM ""apps_app)', ())

q.query.subquery = True
print(q.as_sql(q.query.get_compiler('default'), connection))

# Outputs correct result
('(SELECT ""apps_app"".""id"", ""apps_app"".""name"" FROM ""apps_app"")', ())
}}}

"	Cleanup/optimization	closed	Database layer (models, ORM)	dev	Normal	fixed	subquery, as_sql	Simon Charette	Ready for checkin	1	0	0	0	1	0
