Opened 9 years ago

Last modified 9 years ago

#25772 closed Bug

ArrayField: incorrent len lookup — at Version 1

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 (last modified by mrAdm)

Models:

NullableIntegerArrayModel.objects.create(field=[])
NullableIntegerArrayModel.objects.create(field=[1])
NullableIntegerArrayModel.objects.create(field=[2, 3])
NullableIntegerArrayModel.objects.create(field=[20, 30, 40])

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

Change History (1)

comment:1 by mrAdm, 9 years ago

Description: modified (diff)
Note: See TracTickets for help on using tickets.
Back to Top