Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 174 |
-
def delete_record(container, record):
|
| 175 |
try:
|
| 176 |
-
# Query for the document
|
| 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
|
| 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 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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:
|