Opened 2 years ago

Closed 14 months ago

#29429 closed Bug (needsinfo)

Make len lookup for ArrayField use the array_length function

Reported by: creative3000 Owned by:
Component: contrib.postgres Version: 2.0
Severity: Normal Keywords: db orm ArrayField postgres
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by creative3000)

When you filter by len like in documentation example:


But if you try to annotate something an error pops up example:


It really seems that it should work by default, but instead I needed to use something like this

 Post.objects.all().annotate(tag_len=Func(F('tags'), 1, function='array_length'))

Change History (5)

comment:1 Changed 2 years ago by creative3000

Description: modified (diff)

comment:2 Changed 2 years ago by creative3000

Description: modified (diff)
Owner: changed from nobody to creative3000
Status: newassigned

comment:3 Changed 2 years ago by Tim Graham

Component: Database layer (models, ORM)contrib.postgres
Easy pickings: unset
Summary: Postgres ArrayField doesn't work properly.Make len lookup for ArrayField use the array_length function
Triage Stage: UnreviewedAccepted

I haven't dug into this. It might be fine. Accepting for further investigation.

comment:4 Changed 2 years ago by creative3000

Owner: creative3000 deleted
Status: assignednew

comment:5 Changed 14 months ago by Claude Paroz

Resolution: needsinfo
Status: newclosed

Are you sure the line Post.objects.all().annotate(tag_len=F('tags__len')) is valid?
Where did you read in the documentation a similar construct with a Transform inside a F() expression?

In my opinion, you should have written:

from django.contrib.postgres.fields.array import ArrayLenTransform
Note: See TracTickets for help on using tickets.
Back to Top