Skip to content

Commit 08de220

Browse files
committed
BUG27697947: Collection.add_or_replace_one() ignores the doc_id provided
This patch fixes Collection.add_or_replace_one() method which ignores the doc_id provided. It also removes the restriction of _id modification in DbDoc. Tests were modified for regression.
1 parent 165106d commit 08de220

File tree

4 files changed

+5
-5
lines changed

4 files changed

+5
-5
lines changed

lib/mysqlx/crud.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ def add_or_replace_one(self, doc_id, doc):
400400
"""
401401
if not isinstance(doc, DbDoc):
402402
doc = DbDoc(doc)
403+
doc["_id"] = doc_id
403404
return self.add(doc).upsert(True).execute()
404405

405406
def get_one(self, doc_id):

lib/mysqlx/dbdoc.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ def __str__(self):
5555
return json.dumps(self.__dict__)
5656

5757
def __setitem__(self, index, value):
58-
if index == "_id":
59-
raise ProgrammingError("Cannot modify _id")
6058
self.__dict__[index] = value
6159

6260
def __getitem__(self, index):

lib/mysqlx/statement.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,15 +462,15 @@ def is_upsert(self):
462462
"""
463463
return self._upsert
464464

465-
def upsert(self, val=True):
465+
def upsert(self, value=True):
466466
"""Sets the upset flag to the boolean of the value provided.
467467
Setting of this flag allows updating of the matched rows/documents
468468
with the provided value.
469469
470470
Args:
471-
val (optional[bool]): Set or unset the upsert flag.
471+
value (optional[bool]): Set or unset the upsert flag.
472472
"""
473-
self._upsert = val
473+
self._upsert = value
474474
return self
475475

476476
def execute(self):

tests/test_mysqlx_crud.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,7 @@ def test_add_or_replace_one(self):
13031303
collection.add_or_replace_one("new_id", upsert)
13041304
result = collection.find("age = 99999").execute().fetch_one()
13051305
self.assertEqual("Melissandre", result["name"])
1306+
self.assertEqual("new_id", result["_id"])
13061307

13071308
self.schema.drop_collection(collection_name)
13081309

0 commit comments

Comments
 (0)