I have a script that fetches a certain record, if it exists, and modifies its properties before updating it in the database using replace_item
or upsert_item
. If I fetch the entire record, change the properties and use that record as both the item and body, it works properly.
But, if I decide I to just fetch the ID of the document (to save on RU), put the id into a new object with the adequate properties, then update using the id and new document, the document gets deleted inexplicably.
The problematic variant of the code can be found below:
dbList = list(dbContainer.query_items(
query="SELECT c.id FROM c WHERE c.something = @something",
parameters=[
dict(name='@something', value=whatever)
],
enable_cross_partition_query=True
))
if dbList:
# Update the item if it exists, considering we must have only one
record = dbList[0]
record['property'] = newProperty
try:
container.replace_item(item=record['id'], body=record)
except exceptions.CosmosHttpResponseError as e:
print(f"Failed to update record. Details: {e.message}")
I tried using upsert_item
instead of replace_item
but I get the same outcome. Selecting by ID and setting item=record
gives me a KeyError.