Opened 7 years ago
Last modified 5 weeks ago
#30200 assigned New feature
Add support for using indexes in update() for ArrayFields.
| Reported by: | Mariusz Felisiak | Owned by: | Jacob1507 |
|---|---|---|---|
| Component: | contrib.postgres | Version: | dev |
| Severity: | Normal | Keywords: | ArrayField PostreSQL index update |
| Cc: | Ülgen Sarıkavak, Bhuvnesh | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | yes |
| Needs tests: | yes | Patch needs improvement: | yes |
| Easy pickings: | no | UI/UX: | no |
Description
Django supports index lookup for ArrayFields (see docs). I think we should support them also in update(), e.g.
Foo.objects.update(bar_array_field__0='random text')
Foo.objects.update(bar_array_field__0=F('other_related_field'))
I know that 3rd party packages already support this e.g. django_postgres_extensions, but IMO we should add this to Django.
Change History (13)
comment:1 by , 7 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 6 years ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:5 by , 6 years ago
| Patch needs improvement: | set |
|---|
comment:6 by , 4 years ago
| Has patch: | unset |
|---|---|
| Owner: | changed from to |
| Patch needs improvement: | unset |
comment:7 by , 4 years ago
| Owner: | removed |
|---|---|
| Status: | assigned → new |
comment:8 by , 20 months ago
| Cc: | added |
|---|
comment:9 by , 16 months ago
| Cc: | added |
|---|
comment:10 by , 9 months ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:12 by , 5 weeks ago
| Needs documentation: | set |
|---|---|
| Needs tests: | set |
| Patch needs improvement: | set |
More views are welcome re: whether we should pursue a "left-hand-side" strategy (as in the PR, making ArrayField a compilable) or a "right-hand-side" strategy with slices and ||.
comment:13 by , 5 weeks ago
Oh fun, I missed the review from the previous attempt :-)
Quoting from https://github.com/django/django/pull/11712#discussion_r368243765, on a left-hand-side strategy:
It feels like the new or adjusted API should call a new as_update_sql(lhs) method on the lhs that passes the rhs and expect a (str: sql, Tuple[Any]: params) value to be returned. We have to make sure that by default this method raises by default so transforms like update(dateyear=2020) fail but they could likely be implemented.
That would require UpdateQuery.add_update_values to solve the lookups and resolve both lhs and rhs while disallowing joins and thus aliasing. You can achieve that by sure making UpdateQuery.alias_cols is always False.
OK. Seems reasonable.