Opened 10 years ago
Last modified 6 days ago
#27095 assigned Bug
Queries involving postgres Array fields can't include expressions as elements
| Reported by: | Matthew Wilkes | Owned by: | Ammar Abdur Raheman |
|---|---|---|---|
| Component: | contrib.postgres | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | 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 (13)
comment:1 by , 10 years ago
| Component: | Database layer (models, ORM) → contrib.postgres |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
comment:2 by , 10 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:3 by , 10 years ago
comment:4 by , 10 years ago
comment:5 by , 10 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:6 by , 6 years ago
| Has patch: | set |
|---|---|
| Owner: | set to |
| Status: | new → assigned |
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 , 6 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:9 by , 6 years ago
| Has patch: | unset |
|---|---|
| Triage Stage: | Ready for checkin → Accepted |
Nested arrays still don't work with expressions.
comment:11 by , 5 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:12 by , 6 days ago
| Has patch: | set |
|---|---|
| Status: | new → assigned |
Claiming this and submitting a patch for the remaining nested-array case.
ArrayInLookup now builds an ARRAY[...] expression for nested arrays that contain expressions, reusing the builder extracted from ArrayRHSMixin, and casts it to the left-hand side type. The cast is needed so that an IN list holding both literal and expression arrays resolves to a single type; without it, field__in=[[1], [F("order")]] fails with operator does not exist: bigint[] = integer[]. test_in_including_F_object is no longer marked as an expected failure.
Tests: the full suite passes under SQLite, and postgres_tests, queries, expressions, lookup, annotations, aggregation, model_fields and custom_lookups pass under PostgreSQL 17.
The patch was prepared with assistance from Claude Code (Claude Opus 5); the pull request description records what it was used for.
comment:13 by , 6 days ago
| Owner: | set to |
|---|
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.