#34125: Limitting QuerySet crashes on union() with a single non-empty query
-------------------------------------+-------------------------------------
               Reporter:  th3hamm0r  |          Owner:  nobody
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  dev
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 We have an union-query, that works up until django 4.1.2, but stopped
 working with the latest dev-version of django.
 Basically, one QuerySet is non-empty, and the other one is empty,
 simplified:
 {{{
 q1 = PageLogEntry.objects.all().order_by()
 q2 = q1.union(PageLogEntry.objects.none())
 q2.order_by()[:1]
 }}}

 With PostgreSQL this leads to the following SQL (I've reduced the SQL to
 one field only -> "..."):
 {{{
 (SELECT "wagtailcore_pagelogentry"."id", ... FROM
 "wagtailcore_pagelogentry" LIMIT 1) LIMIT 1;
 }}}

 which results in this error:
 {{{
 psycopg2.errors.SyntaxError: multiple LIMIT clauses not allowed
 LINE 1: ..."."page_id" FROM "wagtailcore_pagelogentry" LIMIT 1) LIMIT 1
 }}}

 With django 4.1.2 the following valid SQL is generated:

 {{{
 (SELECT "wagtailcore_pagelogentry"."id", ... FROM
 "wagtailcore_pagelogentry") LIMIT 1;
 }}}

 With sqlite no errors are thrown in the dev-version.

 Note: In our actual queries, we use ordering (we remove ordering in the
 inner queries with `order_by()` and apply an ordering to the resulting
 union-queryset), but that does not change anything, since the limit seems
 to be the issue, so I've removed ordering to minimize the SQL.

 I've found a similar, fixed bug #32116, but in that case order_by had an
 issue.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34125>
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/010701841a7ae7a3-1bb510d7-74a3-40a9-b120-1902833c0de2-000000%40eu-central-1.amazonses.com.

Reply via email to