Skip to content

Commit 6338305

Browse files
committed
fix array index lookup
1 parent cf39576 commit 6338305

File tree

3 files changed

+7
-18
lines changed

3 files changed

+7
-18
lines changed

django_mongodb/features.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,10 @@ class DatabaseFeatures(BaseDatabaseFeatures):
9191
"model_fields_.test_arrayfield.TestQuerying.test_icontains",
9292
# Field 'field' expected a number but got Value(1).
9393
"model_fields_.test_arrayfield.TestQuerying.test_exact_with_expression",
94-
# int() argument must be a string, a bytes-like object or a real number, not 'list'
95-
"model_fields_.test_arrayfield.TestQuerying.test_index_annotation",
96-
# Wrong results
97-
"model_fields_.test_arrayfield.TestQuerying.test_index",
98-
"model_fields_.test_arrayfield.TestQuerying.test_index_chained",
99-
"model_fields_.test_arrayfield.TestQuerying.test_index_nested",
100-
"model_fields_.test_arrayfield.TestQuerying.test_order_by_slice",
10194
# $lt treats null values as zero.
10295
"model_fields_.test_arrayfield.TestQuerying.test_lt",
10396
"model_fields_.test_arrayfield.TestQuerying.test_len",
97+
"model_fields_.test_arrayfield.TestQuerying.test_index_chained",
10498
# None is $in None
10599
"model_fields_.test_arrayfield.TestQuerying.test_in_as_F_object",
106100
}

django_mongodb/fields/array.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ def get_transform(self, name):
183183
except ValueError:
184184
pass
185185
else:
186-
index += 1 # postgres uses 1-indexing
187186
return IndexTransformFactory(index, self.base_field)
188187
try:
189188
start, end = name.split("_")
@@ -306,10 +305,8 @@ def __init__(self, index, base_field, *args, **kwargs):
306305
self.base_field = base_field
307306

308307
def as_mql(self, compiler, connection):
309-
lhs, params = compiler.compile(self.lhs)
310-
if not lhs.endswith("]"):
311-
lhs = "(%s)" % lhs
312-
return "%s[%%s]" % lhs, (*params, self.index)
308+
lhs_mql = process_lhs(self, compiler, connection)
309+
return {"$arrayElemAt": [lhs_mql, self.index]}
313310

314311
@property
315312
def output_field(self):

tests/model_fields_/test_arrayfield.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ def test_index_nested(self):
356356
instance = NestedIntegerArrayModel.objects.create(field=[[1, 2], [3, 4]])
357357
self.assertSequenceEqual(NestedIntegerArrayModel.objects.filter(field__0__0=1), [instance])
358358

359-
@unittest.expectedFailure
360359
def test_index_used_on_nested_data(self):
361360
instance = NestedIntegerArrayModel.objects.create(field=[[1, 2], [3, 4]])
362361
self.assertSequenceEqual(
@@ -388,7 +387,7 @@ def test_slice(self):
388387
NullableIntegerArrayModel.objects.filter(field__0_2=[2, 3]), self.objs[2:3]
389388
)
390389

391-
def test_order_by_slice(self):
390+
def test_order_by_index(self):
392391
more_objs = (
393392
NullableIntegerArrayModel.objects.create(field=[1, 637]),
394393
NullableIntegerArrayModel.objects.create(field=[2, 1]),
@@ -398,19 +397,18 @@ def test_order_by_slice(self):
398397
self.assertSequenceEqual(
399398
NullableIntegerArrayModel.objects.order_by("field__1"),
400399
[
400+
self.objs[0],
401+
self.objs[1],
402+
self.objs[4],
401403
more_objs[2],
402404
more_objs[1],
403405
more_objs[3],
404406
self.objs[2],
405407
self.objs[3],
406408
more_objs[0],
407-
self.objs[4],
408-
self.objs[1],
409-
self.objs[0],
410409
],
411410
)
412411

413-
@unittest.expectedFailure
414412
def test_slice_nested(self):
415413
instance = NestedIntegerArrayModel.objects.create(field=[[1, 2], [3, 4]])
416414
self.assertSequenceEqual(

0 commit comments

Comments
 (0)