#34945: annotate -> union -> values gives wrong values
-------------------------------------+-------------------------------------
               Reporter:  Tom        |          Owner:  nobody
  Carrick                            |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  4.2
  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          |
-------------------------------------+-------------------------------------
 With the following code:

 {{{
 import uuid

 from django.conf import settings
 from django.contrib.auth import get_user_model
 from django.db import models
 from django.db.models.functions import Cast


 class DocumentQuerySet(models.QuerySet):
     def for_user(self, user):
         return self.filter(things__user=user).union(
             self.filter(other_things__user=user)
         )


 class Document(models.Model):
     name = models.CharField()
     id = models.UUIDField(default=uuid.uuid4, primary_key=True)

     objects = DocumentQuerySet().as_manager()


 class Thing(models.Model):
     user = models.ForeignKey(
         settings.AUTH_USER_MODEL, related_name="things",
 on_delete=models.CASCADE
     )
     document = models.ForeignKey(
         Document, related_name="things", on_delete=models.CASCADE
     )


 class OtherThing(models.Model):
     user = models.ForeignKey(
         settings.AUTH_USER_MODEL, related_name="other_things",
 on_delete=models.CASCADE
     )
     document = models.ForeignKey(
         Document, related_name="other_things", on_delete=models.CASCADE
     )


 def broken():
     user = get_user_model().objects.first()
     return Document.objects.annotate(
         pk_str=Cast("pk", output_field=models.CharField())
     ).for_user(user).values_list("pk_str", flat=True)
 }}}

 Running `broken()`, I would expect to say the stringified UUIDs from the
 Document model. Instead I get the `name`s. It also happens when using
 `values()`. If not using either, it works just fine, and you can access
 `document.pk_str` and it works just fine. After playing with this, it
 seems to be because it's defined first in the model. If I move `id` to the
 top, it actually returns the UUIDs, without the cast being applied. The
 query (as far as I can tell from Django) looks fine:

 {{{
 (SELECT "testapp_document"."id" AS "col1", "testapp_document"."name" AS
 "col2", ("testapp_document"."id")::varchar AS "pk_str" FROM
 "testapp_document" INNER JOIN "testapp_thing" ON ("testapp_document"."id"
 = "testapp_thing"."document_id") WHERE "testapp_thing"."user_id" = 1)
 UNION (SELECT "testapp_document"."id" AS "col1", "testapp_document"."name"
 AS "col2", ("testapp_document"."id")::varchar AS "pk_str" FROM
 "testapp_document" INNER JOIN "testapp_otherthing" ON
 ("testapp_document"."id" = "testapp_otherthing"."document_id") WHERE
 "testapp_otherthing"."user_id" = 1)
 }}}

 And logged from Postgres also looks correct:
 {{{
 (SELECT "testapp_document"."name" AS "col1", "testapp_document"."id" AS
 "col2", ("testapp_document"."id")::varchar AS "pk_str" FROM
 "testapp_document" INNER JOIN "testapp_thing" ON ("testapp_document"."id"
 = "testapp_thing"."document_id") WHERE "testapp_thing"."user_id" = 1)
 UNION (SELECT "testapp_document"."name" AS "col1", "testapp_document"."id"
 AS "col2", ("testapp_document"."id")::varchar AS "pk_str" FROM
 "testapp_document" INNER JOIN "testapp_otherthing" ON
 ("testapp_document"."id" = "testapp_otherthing"."document_id") WHERE
 "testapp_otherthing"."user_id" = 1) LIMIT 21
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34945>
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/0107018b9f84e49f-4e20db75-bf37-4033-8bd3-62dc39ed0b95-000000%40eu-central-1.amazonses.com.

Reply via email to