#34594: KT doesn’t work across foreign keys
-------------------------------------+-------------------------------------
               Reporter:  Roman      |          Owner:  nobody
  Odaisky                            |
                   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          |
-------------------------------------+-------------------------------------
 Assuming
 {{{
 class Parent(Model):
     data = JSONField()

 class Child(Model):
     parent = ForeignKey(Parent)
 }}}
 the expressions
 {{{
 Parent.objects.annotate(x=F("data__some_string")).first().x
 Parent.objects.annotate(x=KT("data__some_string")).first().x
 Parent.objects.first().data["some_string"]
 }}}
 all return the same value. So far, so good. However, in the following F
 works but KT doesn’t:
 {{{
 Child.objects.annotate(x=F("parent__data__some_string"))
 Child.objects.annotate(x=KT("parent__data__some_string"))
 }}}
 The problem is in the SQL that KT generates: `("parent"."id" #>>
 ARRAY['data','some_string']) AS "x"`, which, it seems, is the result of
 KeyTextTransform.from_lookup taking all the double-underscore-separated
 parts of the argument of KT to mean JSON lookups and not other possible
 lookups such as foreign keys.

 Please fix by using the same mechanisms F uses in this case.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34594>
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/010701884bc9c8c1-5c8e1c39-e83d-4039-98dd-9fdf4a183e32-000000%40eu-central-1.amazonses.com.

Reply via email to