[Django] #34594: KT doesn’t work across foreign keys

7 views
Skip to first unread message

Django

unread,
May 23, 2023, 11:24:59 PM5/23/23
to django-...@googlegroups.com
#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.

Django

unread,
May 23, 2023, 11:56:17 PM5/23/23
to django-...@googlegroups.com
#34594: KT doesn’t work across foreign keys
-------------------------------------+-------------------------------------
Reporter: Roman Odaisky | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* type: Bug => New feature
* resolution: => wontfix


Comment:

> 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.

Thanks for the report, however, it works exactly as
[https://docs.djangoproject.com/en/stable/topics/db/queries/#django.db.models.fields.json.KT
documented] ''"You can use the double underscore notation in `lookup` to
chain dictionary key and index transforms."'', so it's not a bug but a new
feature request. As far as I'm aware we don't want to complicate it, and
you can annotate nested `JSONField` to make it work, e.g.
{{{
Child.objects.annotate(data=F("parent__data"), x=KT("data__some_string"))
}}}
You can start a discussion on DevelopersMailingList if you don't agree.

--
Ticket URL: <https://code.djangoproject.com/ticket/34594#comment:1>

Django

unread,
Jun 20, 2023, 9:59:39 AM6/20/23
to django-...@googlegroups.com
#34594: KT doesn’t work across foreign keys
-------------------------------------+-------------------------------------
Reporter: Roman Odaisky | Owner: nobody
Type: New feature | Status: closed
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Natalia Bidart):

I think #34667 is a duplicate of this one.

--
Ticket URL: <https://code.djangoproject.com/ticket/34594#comment:2>

Reply all
Reply to author
Forward
0 new messages