#25772: ArrayField: incorrent len lookup ----------------------------------+-------------------------------------- Reporter: mrAdm | Owner: Type: Bug | Status: new Component: contrib.postgres | Version: 1.8 Severity: Normal | Resolution: Keywords: | Triage Stage: Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 ----------------------------------+-------------------------------------- Changes (by mrAdm):
* needs_better_patch: => 0 * needs_tests: => 0 * needs_docs: => 0 Old 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 > }}} New description: 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 }}} -- -- Ticket URL: <https://code.djangoproject.com/ticket/25772#comment:1> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/063.a9c6c6101dc1e9962114f2160a0bae2e%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.