Opened 9 years ago
Last modified 9 years ago
#25772 closed Bug
ArrayField: incorrent len lookup — at Initial Version
Reported by: | mrAdm | Owned by: | |
---|---|---|---|
Component: | contrib.postgres | Version: | 1.8 |
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
Models:
NullableIntegerArrayModel.objects.create(field=[]) NullableIntegerArrayModel.objects.create(field=[1]) NullableIntegerArrayModel.objects.create(field=[2, 3]) NullableIntegerArrayModel.objects.create(field=[20, 30, 40]) NullableIntegerArrayModel.objects.create(field=None)
Queries:
NullableIntegerArrayModel.objects.filter(field__len=0) # return: empty queryset NullableIntegerArrayModel.objects.filter(field__len__lt=2) # return only one model (field=[1])
Reason:
PostgreSQL function array_length(ARRAY[]::int4[], 1) return null if array empty.
The solution is to use a function PostgreSQL: COALESCE ().
SELECT * FROM ... WHERE COALESCE(array_length("field", 1), 0)=0
Note:
See TracTickets
for help on using tickets.