#31977: Transforms silently don't work in F and OuterRef expressions.
-------------------------------------+-------------------------------------
               Reporter:  Gordon     |          Owner:  nobody
  Wrigley                            |
                   Type:             |         Status:  new
  Uncategorized                      |
              Component:  Database   |        Version:  3.1
  layer (models, ORM)                |       Keywords:  function, lookup,
               Severity:  Normal     |  transform, F, OuterRef
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I've seen this on different databases and different Django versions.

 Here's Sqlite with Django 3.1.1

 {{{#!python
 >>> qs =
 Order.objects.filter(submitted_time__date=F("submitted_time__date"))
 >>> qs
 <QuerySet []>
 >>> print(qs.query)
 SELECT <REDACTED> FROM "core_order" WHERE
 django_datetime_cast_date("core_order"."submitted_time", 'UTC', 'UTC') =
 "core_order"."submitted_time"
 }}}

 With a JSONField

 {{{#!python
 >>> qs = Order.objects.filter(cruft__bob=F("cruft__fred"))
 >>> print(qs.query)
 SELECT <REDACTED> FROM "core_order" WHERE
 JSON_EXTRACT("core_order"."cruft", $."bob") = "core_order"."cruft"
 }}}

 With JSON in an OuterRef

 {{{#!python
 >>> qs =
 
Order.objects.annotate(prev=Subquery(Order.objects.filter(cruft__bob=OuterRef("cruft__fred")).values("pk")[:1]))
 >>> print(qs.query)
 SELECT <REDACTED>, (SELECT U0."id" FROM "core_order" U0 WHERE
 JSON_EXTRACT(U0."cruft", $."bob") = "core_order"."cruft" LIMIT 1) AS
 "prev" FROM "core_order"
 }}}

 And on Postgres

 {{{#!python
 >>> qs =
 Order.objects.filter(submitted_time__date=F("submitted_time__date"))
 >>> qs
 <QuerySet []>
 >>> print(qs.query)
 SELECT <REDACTED> FROM "core_order" WHERE ("core_order"."submitted_time"
 AT TIME ZONE 'UTC')::date = "core_order"."submitted_time"
 }}}

 It does the same thing on Django 2.2 as well.

 All these failures are totally silent, you can create and evaluate the
 queryset and the only indication that something is amiss is you get the
 wrong results. And I couldn't find any reference to this behaviour in the
 documentation or other tickets, although it's not an easy thing to search
 for.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31977>
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/050.ae5f0bb57677d9723f82ab2659e899b1%40djangoproject.com.

Reply via email to