Skip to content

Commit c4808f4

Browse files
committed
cqlengine: fix map remove feature for non-nullable values
following code: ``` .update( bin_map__remove={123, 456} ) ``` Would fail if map is defined with non-nullable value type: ``` columns.Map(columns.BigInt, columns.Bytes, required=False, default={}) ```
1 parent c5bed09 commit c4808f4

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

cassandra/cqlengine/statements.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,9 @@ def update_context_id(self, i):
821821
self.context_counter += conditional.get_context_size()
822822

823823
def add_update(self, column, value, operation=None, previous=None):
824-
value = column.to_database(value)
824+
# For remove all values are None, no need to convert them
825+
if operation != 'remove':
826+
value = column.to_database(value)
825827
col_type = type(column)
826828
container_update_type = ContainerUpdateClause.type_map.get(col_type)
827829
if container_update_type:

tests/integration/cqlengine/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class TestQueryUpdateModel(Model):
3030
text_set = columns.Set(columns.Text, required=False)
3131
text_list = columns.List(columns.Text, required=False)
3232
text_map = columns.Map(columns.Text, columns.Text, required=False)
33+
bin_map = columns.Map(columns.BigInt, columns.Bytes, required=False, default={})
34+
3335

3436
class BaseCassEngTestCase(unittest.TestCase):
3537

tests/integration/cqlengine/query/test_updates.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,30 @@ def test_map_update_remove(self):
246246
TestQueryUpdateModel.objects.create(
247247
partition=partition,
248248
cluster=cluster,
249-
text_map={"foo": '1', "bar": '2'}
249+
text_map={"foo": '1', "bar": '2'},
250+
bin_map={123: b'1', 789: b'2'}
250251
)
251252
TestQueryUpdateModel.objects(partition=partition, cluster=cluster).update(
252253
text_map__remove={"bar"},
253-
text_map__update={"foz": '4', "foo": '2'}
254+
text_map__update={"foz": '4', "foo": '2'},
255+
bin_map__remove={789},
256+
bin_map__update={456: b'4', 123: b'2'}
254257
)
255258
obj = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
256259
self.assertEqual(obj.text_map, {"foo": '2', "foz": '4'})
260+
self.assertEqual(obj.bin_map, {123: b'2', 456: b'4'})
257261

258262
TestQueryUpdateModel.objects(partition=partition, cluster=cluster).update(
259-
text_map__remove={"foo", "foz"}
263+
text_map__remove={"foo", "foz"},
264+
bin_map__remove={123, 456}
265+
)
266+
rec = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
267+
self.assertEqual(
268+
rec.text_map,
269+
{}
260270
)
261271
self.assertEqual(
262-
TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster).text_map,
272+
rec.bin_map,
263273
{}
264274
)
265275

0 commit comments

Comments
 (0)