Opened 8 years ago

Last modified 3 years ago

#27095 new Bug

Queries involving postgres Array fields can't include expressions as elements

Reported by: Matthew Wilkes Owned by:
Component: contrib.postgres Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Issue #22288 allows use of expressions in lookups that take iterables rather than single items directly. This allows, for example:

NullableIntegerArrayModel.objects.filter(field__in=[models.F('field')]),

i.e., filtering an ArrayField using __in with expressions that evaluate to other ArrayField references.

However, the following fails:

NullableIntegerArrayModel.objects.filter(field__in=[[models.F('id')]]),

i.e., building a new array that contains scalar expressions.

There are some tests, marked with expectedFailure, in postgres_tests.test_array: test_in_including_F_object and test_contained_by_including_F_object, that demonstrate this problem.

Change History (11)

comment:1 by Tim Graham, 8 years ago

Component: Database layer (models, ORM)contrib.postgres
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

comment:2 by PREMANAND, 8 years ago

Owner: changed from nobody to PREMANAND
Status: newassigned

comment:3 by PREMANAND, 8 years ago

Im unable to locate the postgres_tests.test_array: test_in_including_F_object and test_contained_by_including_F_object in the master branch.

Can you please point me to the correct location?

It would be great if you can build an example, so we both are on the same page.

comment:5 by PREMANAND, 8 years ago

Owner: PREMANAND removed
Status: assignednew

comment:6 by Hannes Ljungberg, 4 years ago

Has patch: set
Owner: set to Hannes Ljungberg
Status: newassigned

I've created a PR (https://github.com/django/django/pull/13680) to fix the issue of allowing expressions on ArrayField lookups where the right-hand side is a list. Note that this PR does not fix the issue of expressions in nested lists, it should in my opinion be tracked as a separate ticket. I'm not really familiar with the process of splitting up tickets, would it make sense to create a new ticket for the nested expressions in the case of this PR being merged?

comment:7 by Mariusz Felisiak, 4 years ago

Triage Stage: AcceptedReady for checkin

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

In 33403bf8:

Refs #27095 -- Allowed (non-nested) arrays containing expressions for ArrayField lookups.

comment:9 by Mariusz Felisiak, 4 years ago

Has patch: unset
Triage Stage: Ready for checkinAccepted

Nested arrays still don't work with expressions.

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

In d746f289:

Refs #27095 -- Fixed test_contained_by_including_F_object when run in reverse.

Tests should not rely on auto PKs.

Test regression in 33403bf80f635577a18426bc99c8a65e31fd8dfa.

comment:11 by Mariusz Felisiak, 3 years ago

Owner: Hannes Ljungberg removed
Status: assignednew
Note: See TracTickets for help on using tickets.
Back to Top