Opened 7 years ago

Last modified 13 days 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 Carlton Gibson, 7 years ago

Triage Stage: UnreviewedAccepted

OK. Seems reasonable.

comment:2 by Tim Graham, 7 years ago

See also #24709 for using QuerySet.update() without indexes.

comment:3 by Nasir Hussain, 6 years ago

Owner: set to Nasir Hussain
Status: newassigned

comment:4 by Nasir Hussain, 6 years ago

Has patch: set

Created a PR.

comment:5 by Mariusz Felisiak, 6 years ago

Patch needs improvement: set

comment:6 by Abhyudai, 4 years ago

Has patch: unset
Owner: changed from Nasir Hussain to Abhyudai
Patch needs improvement: unset

comment:7 by Mariusz Felisiak, 4 years ago

Owner: Abhyudai removed
Status: assignednew

comment:8 by Ülgen Sarıkavak, 19 months ago

Cc: Ülgen Sarıkavak added

comment:9 by Bhuvnesh, 15 months ago

Cc: Bhuvnesh added

comment:10 by Jacob1507, 8 months ago

Owner: set to Jacob1507
Status: newassigned

comment:11 by Jacob Walls, 8 months ago

Has patch: set

comment:12 by Jacob Walls, 13 days 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 Jacob Walls, 13 days 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.

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