#33435: Since django 3.0 Subquery.as_sql() generates invalid SQL
-------------------------------------+-------------------------------------
     Reporter:  M1ha Shvn            |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  subquery, as_sql     |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Description changed by M1ha Shvn:

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>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/067.1ee5d03ade909f5423e120f0dd86bc91%40djangoproject.com.

Reply via email to