{{{
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)', ())
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33435>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Old description:
> 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:
>
> {{{
> 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)', ())
> }}}
New description:
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")', ())
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/33435#comment:1>
* cc: Simon Charette (added)
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted
Comment:
Sounds reasonable.
--
Ticket URL: <https://code.djangoproject.com/ticket/33435#comment:2>
Comment (by Simon Charette):
Sounds reasonable to me as well, I'd only suggest we `.clone()` the
`query` before altering though.
--
Ticket URL: <https://code.djangoproject.com/ticket/33435#comment:3>
* owner: nobody => Mohamed Nabil Rady
* status: new => assigned
* has_patch: 0 => 1
* easy: 0 => 1
Comment:
Pull request: https://github.com/django/django/pull/15320.
Ran back-end tests and they went fine.
--
Ticket URL: <https://code.djangoproject.com/ticket/33435#comment:4>
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/33435#comment:5>
* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/33435#comment:6>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"f37face331f21cb8af70fc4ec101ec7b6be1f63e" f37face3]:
{{{
#!CommitTicketReference repository=""
revision="f37face331f21cb8af70fc4ec101ec7b6be1f63e"
Fixed #33435 -- Fixed invalid SQL generatered by Subquery.as_sql().
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/33435#comment:7>