awacke1 commited on
Commit
afddcdf
ยท
verified ยท
1 Parent(s): d66b597

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -6
app.py CHANGED
@@ -170,20 +170,33 @@ def update_record(container, updated_record):
170
  except Exception as e:
171
  return False, f"Error: {traceback.format_exc()} ๐Ÿ˜ฑ"
172
 
173
- # ๐Ÿ—‘๏ธ Delete record โ€“ now explicitly catches resource not found errors
174
  def delete_record(container, record):
175
  try:
176
- container.delete_item(item=record['id'], partition_key=record['id'])
177
- return True, f"Record {record['id']} successfully deleted. ๐Ÿ—‘๏ธ"
178
- except exceptions.CosmosResourceNotFoundError:
179
- # Document does not exist โ€“ treat as deleted
180
- return True, f"Record {record['id']} not found (already deleted). ๐Ÿ—‘๏ธ"
 
 
 
 
 
 
 
 
 
 
 
 
181
  except exceptions.CosmosHttpResponseError as e:
182
  return False, f"HTTP error: {str(e)} ๐Ÿšจ"
183
  except Exception as e:
184
  return False, f"Unexpected error: {traceback.format_exc()} ๐Ÿ˜ฑ"
185
 
186
 
 
187
  # ๐Ÿ’พ Save a new document to Cosmos DB with extra fields
188
  def save_to_cosmos_db(container, query, response1, response2):
189
  try:
 
170
  except Exception as e:
171
  return False, f"Error: {traceback.format_exc()} ๐Ÿ˜ฑ"
172
 
173
+ # ๐Ÿ—‘๏ธ Delete record โ€“ query first, then delete using the actual partition key
174
  def delete_record(container, record):
175
  try:
176
+ # Query for the document by id to get the actual partition key value.
177
+ query = "SELECT * FROM c WHERE c.id=@id"
178
+ parameters = [{"name": "@id", "value": record["id"]}]
179
+ docs = list(container.query_items(
180
+ query=query,
181
+ parameters=parameters,
182
+ enable_cross_partition_query=True
183
+ ))
184
+ if not docs:
185
+ # Document doesn't exist; treat as already deleted.
186
+ return True, f"Record {record['id']} not found (already deleted). ๐Ÿ—‘๏ธ"
187
+
188
+ # Use the partition key value from the retrieved document.
189
+ doc = docs[0]
190
+ partition_key = doc.get("name") # assuming 'name' is your partition key
191
+ container.delete_item(item=doc["id"], partition_key=partition_key)
192
+ return True, f"Record {doc['id']} successfully deleted. ๐Ÿ—‘๏ธ"
193
  except exceptions.CosmosHttpResponseError as e:
194
  return False, f"HTTP error: {str(e)} ๐Ÿšจ"
195
  except Exception as e:
196
  return False, f"Unexpected error: {traceback.format_exc()} ๐Ÿ˜ฑ"
197
 
198
 
199
+
200
  # ๐Ÿ’พ Save a new document to Cosmos DB with extra fields
201
  def save_to_cosmos_db(container, query, response1, response2):
202
  try: