Skip to content

Commit fa61b48

Browse files
add batch operation / exception instructions
1 parent b3f5731 commit fa61b48

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

README.mkd

+47
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,53 @@ You can also mix `save` and `delete` operations in the same query as follows (no
293293
batcher.batch([score1.save, score2.save, score3.delete])
294294
~~~~~
295295

296+
If an error occurs during one or multiple of the operations, it will not affect
297+
the execution of the remaining operations. Instead, the `batcher.batch_save` or
298+
`batcher.batch_delete` or `batcher.batch` will raise a `ParseBatchError`
299+
(child of `ParseError`) exception with `.message` set to a *list* of the errors
300+
encountered. For example:
301+
302+
~~~~~ {python}
303+
# Batch save a list of two objects:
304+
# dupe_object is a duplicate violating a unique key constraint
305+
# dupe_object2 is a duplicate violating a unique key constraint
306+
# new_object is a new object satisfying the unique key constraint
307+
#
308+
# dupe_object and dupe_object2 will fail to save, and new_object will save successfully
309+
310+
dupe_object = list(MyClass.Query.all().limit(2))[0]
311+
dupe_object2 = list(MyClass.Query.all().limit(2))[1]
312+
new_object = MyClass(some_column=11111)
313+
objects = [dupe_object + new_object]
314+
315+
batcher = ParseBatcher()
316+
batcher.batch_save(objects)
317+
~~~~~
318+
319+
will raise an exception:
320+
321+
~~~~~ {python}
322+
Traceback (most recent call last):
323+
File "<console>", line 1, in <module>
324+
File "/Users/miles/ParsePy/parse_rest/connection.py", line 199, in batch_save
325+
self.batch(o.save for o in objects)
326+
File "/Users/miles/ParsePy/parse_rest/connection.py", line 195, in batch
327+
raise core.ParseBatchError(batched_errors)
328+
329+
ParseBatchError: [{u'code': 11000, u'error': u'E11000 duplicate key error index: myapp.MyClass.$my_column_1 dup key: { : 555555 }'}, {u'code': 11000, u'error': u'E11000 duplicate key error index: myapp.MyClass.$my_column_1 dup key: { : 44444 }'}]
330+
~~~~~
331+
332+
And `CRUCIALLY`, the objectId field of the NON-duplicate object will be correctly set:
333+
334+
~~~~~ {python}
335+
>>> #batch_save as above...
336+
>>> print objects
337+
[<MyClass:gOHuhPbGZJ>, <MyClass:None>, <MyClass:None>]
338+
~~~~~
339+
340+
Therefore, one way to tell which objects saved successfully after a batch save operation
341+
is to check which objects have `objectId` set.
342+
296343
Querying
297344
--------
298345

0 commit comments

Comments
 (0)