Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#31829 closed Bug (fixed)

Chaining KeyTransform with 'contains' lookup uses builtin lookup instead of overridden lookup

Reported by: Sage Abdullah Owned by: Sage Abdullah
Component: Database layer (models, ORM) Version: 3.1
Severity: Release blocker Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Sage Abdullah)

The previous implementation of django.contrib.postgres.fields.JSONField, when chained with a KeyTransform and a contains lookup, uses the overridden contains lookup that is JSON-based and not the builtin one that is pattern-based.

Using the example model in the 3.1 release notes,

class ContactInfo(models.Model):
    data = models.JSONField()

obj = ContactInfo.objects.create(data={
    'name': 'John',
    'cities': ['London', 'Cambridge'],
    'pets': {'dogs': ['Rufus', 'Meg']},
})
ContactInfo.objects.filter(
    data__cities__contains='Cambridge',
)

The query returns a queryset with obj in it, which is expected.

However, the following query:

ContactInfo.objects.filter(
    data__cities__contains='bridge',
)

Also returns a queryset with obj in it. Using the previous implementation, obj doesn't match the query because contains uses JSON-based containment checking. That is, it checks whether the array in data__cities contains an element that's exactly "bridge".

Change History (9)

comment:1 by Sage Abdullah, 4 years ago

Description: modified (diff)

comment:2 by Sage Abdullah, 4 years ago

Owner: changed from nobody to Sage Abdullah
Status: newassigned

comment:3 by Sage Abdullah, 4 years ago

Has patch: set

comment:4 by Mariusz Felisiak, 4 years ago

Patch needs improvement: set
Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted
Version: master3.1

comment:5 by Mariusz Felisiak, 4 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

Resolution: fixed
Status: assignedclosed

In 2d8dcba0:

Fixed #31829 -- Used JSONField contains lookup on key transforms.

comment:7 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 23ce3d8:

[3.1.x] Fixed #31829 -- Used JSONField contains lookup on key transforms.

Backport of 2d8dcba03aae200aaa103ec1e69f0a0038ec2f85 from master

comment:8 by GitHub <noreply@…>, 4 years ago

In 184a6eeb:

Refs #31829 -- Added DatabaseFeatures.json_key_contains_list_matching_requires_list.

CockroachDB's behavior matches PostgreSQL.

comment:9 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 32cb1fe1:

[3.1.x] Refs #31829 -- Added DatabaseFeatures.json_key_contains_list_matching_requires_list.

CockroachDB's behavior matches PostgreSQL.
Backport of 184a6eebb0ef56d5f1b1315a8e666830e37f3f81 from master

Note: See TracTickets for help on using tickets.
Back to Top