Skip to content

Commit

Permalink
Fixed #30715 -- Fixed crash of ArrayField lookups on ArrayAgg annotat…
Browse files Browse the repository at this point in the history
…ions over AutoField.
  • Loading branch information
felixxm authored and carltongibson committed Aug 23, 2019
1 parent b1f6694 commit 521308e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
6 changes: 5 additions & 1 deletion django/contrib/postgres/fields/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def db_type(self, connection):
size = self.size or ''
return '%s[%s]' % (self.base_field.db_type(connection), size)

def cast_db_type(self, connection):
size = self.size or ''
return '%s[%s]' % (self.base_field.cast_db_type(connection), size)

def get_placeholder(self, value, compiler, connection):
return '%s::{}'.format(self.db_type(connection))

Expand Down Expand Up @@ -193,7 +197,7 @@ def formfield(self, **kwargs):
class ArrayCastRHSMixin:
def process_rhs(self, compiler, connection):
rhs, rhs_params = super().process_rhs(compiler, connection)
cast_type = self.lhs.output_field.db_type(connection)
cast_type = self.lhs.output_field.cast_db_type(connection)
return '%s::%s' % (rhs, cast_type), rhs_params


Expand Down
22 changes: 22 additions & 0 deletions tests/postgres_tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
)

try:
from django.contrib.postgres.aggregates import ArrayAgg
from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.fields.array import IndexTransform, SliceTransform
from django.contrib.postgres.forms import (
Expand Down Expand Up @@ -280,6 +281,27 @@ def test_overlap_charfield(self):
[]
)

def test_lookups_autofield_array(self):
qs = NullableIntegerArrayModel.objects.filter(
field__0__isnull=False,
).values('field__0').annotate(
arrayagg=ArrayAgg('id'),
).order_by('field__0')
tests = (
('contained_by', [self.objs[1].pk, self.objs[2].pk, 0], [2]),
('contains', [self.objs[2].pk], [2]),
('exact', [self.objs[3].pk], [20]),
('overlap', [self.objs[1].pk, self.objs[3].pk], [2, 20]),
)
for lookup, value, expected in tests:
with self.subTest(lookup=lookup):
self.assertSequenceEqual(
qs.filter(
**{'arrayagg__' + lookup: value},
).values_list('field__0', flat=True),
expected,
)

def test_index(self):
self.assertSequenceEqual(
NullableIntegerArrayModel.objects.filter(field__0=2),
Expand Down

0 comments on commit 521308e

Please sign in to comment.