#28781: Order of queryset.union() with queryset.values() is incorrect
-------------------------------------+-------------------------------------
     Reporter:  Amir Aziiev          |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.11
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  union, values        |             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 Amir Aziiev:

Old description:

> For example:
>
> {{{
> from django.db import models
>

> class MyModel(models.Model):
>     col1 = models.CharField(max_length=11)
>     col2 = models.CharField(max_length=22)
>     col3 = models.CharField(max_length=33)
>

> qs = MyModel.objects.union(MyModel.objects.all(),
> all=True).values('col2', 'col3', 'col1', 'id').order_by('col1')
> print(qs.query)
> # Expected SQL
> """
> (SELECT "mymodel"."id", "mymodel"."col1", "mymodel"."col2",
> "mymodel"."col3" FROM "mymodel")
>   UNION ALL
> (SELECT"mymodel"." id", "mymodel"."col1", "mymodel"."col2",
> "mymodel"."col3" FROM "mymodel")
>   ORDER BY (3);
> ------------^
> """
> # Must be SQL
> """
> (SELECT "mymodel"."id", "mymodel"."col1", "mymodel"."col2",
> "mymodel"."col3" FROM "mymodel")
>   UNION ALL
> (SELECT"mymodel"." id", "mymodel"."col1", "mymodel"."col2",
> "mymodel"."col3" FROM "mymodel")
>   ORDER BY (2);
> ------------^
> }}}

New description:

 For example:

 {{{
 from django.db import models


 class MyModel(models.Model):
     col1 = models.CharField(max_length=11)
     col2 = models.CharField(max_length=22)
     col3 = models.CharField(max_length=33)


 qs = MyModel.objects.union(MyModel.objects.all(), all=True).values('col2',
 'col3', 'col1', 'id').order_by('col1')
 print(qs.query)
 # Expected SQL
 """
 (SELECT "mymodel"."id", "mymodel"."col1", "mymodel"."col2",
 "mymodel"."col3" FROM "mymodel")
   UNION ALL
 (SELECT"mymodel"." id", "mymodel"."col1", "mymodel"."col2",
 "mymodel"."col3" FROM "mymodel")
   ORDER BY (3);
 ------------^
 """
 # Must be SQL
 """
 (SELECT "mymodel"."id", "mymodel"."col1", "mymodel"."col2",
 "mymodel"."col3" FROM "mymodel")
   UNION ALL
 (SELECT"mymodel"." id", "mymodel"."col1", "mymodel"."col2",
 "mymodel"."col3" FROM "mymodel")
   ORDER BY (2);
 ------------^
 """
 }}}

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28781#comment:2>
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 post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/065.b4e47cd4aaf4271370a737eea59bccd9%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to