awacke1 commited on
Commit
0d64ea2
ยท
verified ยท
1 Parent(s): 24660c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -170,10 +170,10 @@ def update_record(container, updated_record):
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(
@@ -182,13 +182,17 @@ def delete_record(container, record):
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)} ๐Ÿšจ"
@@ -196,7 +200,6 @@ def delete_record(container, record):
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:
 
170
  except Exception as e:
171
  return False, f"Error: {traceback.format_exc()} ๐Ÿ˜ฑ"
172
 
173
+ # ๐Ÿ—‘๏ธ Delete record โ€“ query first to get the proper partition key value from "testPART"
174
+ def delete_record(container, record, partition_key_field="testPART"):
175
  try:
176
+ # Query for the document using its id
177
  query = "SELECT * FROM c WHERE c.id=@id"
178
  parameters = [{"name": "@id", "value": record["id"]}]
179
  docs = list(container.query_items(
 
182
  enable_cross_partition_query=True
183
  ))
184
  if not docs:
185
+ # Document not found; treat as already deleted
186
  return True, f"Record {record['id']} not found (already deleted). ๐Ÿ—‘๏ธ"
187
 
 
188
  doc = docs[0]
189
+ # Retrieve the partition key value from the document (adjust field name as needed)
190
+ partition_key_value = doc.get(partition_key_field)
191
+ if partition_key_value is None:
192
+ return False, f"Partition key field '{partition_key_field}' not found in document {doc['id']}."
193
+
194
+ # Delete the document using its id and the actual partition key value
195
+ container.delete_item(item=doc["id"], partition_key=partition_key_value)
196
  return True, f"Record {doc['id']} successfully deleted. ๐Ÿ—‘๏ธ"
197
  except exceptions.CosmosHttpResponseError as e:
198
  return False, f"HTTP error: {str(e)} ๐Ÿšจ"
 
200
  return False, f"Unexpected error: {traceback.format_exc()} ๐Ÿ˜ฑ"
201
 
202
 
 
203
  # ๐Ÿ’พ Save a new document to Cosmos DB with extra fields
204
  def save_to_cosmos_db(container, query, response1, response2):
205
  try: