Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -176,11 +176,32 @@ def update_record(container, updated_record):
|
|
| 176 |
# 🗑️ Delete record - Saying goodbye to data (it's not you, it's me)
|
| 177 |
def delete_record(container, id):
|
| 178 |
try:
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
except exceptions.CosmosHttpResponseError as e:
|
| 185 |
return False, f"HTTP error occurred: {str(e)} 🚨"
|
| 186 |
except Exception as e:
|
|
|
|
| 176 |
# 🗑️ Delete record - Saying goodbye to data (it's not you, it's me)
|
| 177 |
def delete_record(container, id):
|
| 178 |
try:
|
| 179 |
+
# First query to verify document exists and get partition key
|
| 180 |
+
query = "SELECT c.id FROM c WHERE c.id = @id"
|
| 181 |
+
params = [{"name": "@id", "value": id}]
|
| 182 |
+
|
| 183 |
+
# Execute query with cross partition enabled
|
| 184 |
+
items = list(container.query_items(
|
| 185 |
+
query=query,
|
| 186 |
+
parameters=params,
|
| 187 |
+
enable_cross_partition_query=True
|
| 188 |
+
))
|
| 189 |
+
|
| 190 |
+
if not items:
|
| 191 |
+
return False, f"Record with id {id} not found. 🕵️♂️"
|
| 192 |
+
|
| 193 |
+
# Get partition key value from the query result
|
| 194 |
+
partition_key = items[0]['id']
|
| 195 |
+
|
| 196 |
+
# Create partition key instance
|
| 197 |
+
pk = PartitionKey(partition_key)
|
| 198 |
+
|
| 199 |
+
# Delete the document using correct partition key
|
| 200 |
+
container.delete_item(item=id, partition_key=pk)
|
| 201 |
+
return True, f"Successfully deleted record with id: {id} 🗑️"
|
| 202 |
+
|
| 203 |
+
except exceptions.CosmosResourceNotFoundError:
|
| 204 |
+
return False, f"Record with id {id} not found. 🕵️♂️"
|
| 205 |
except exceptions.CosmosHttpResponseError as e:
|
| 206 |
return False, f"HTTP error occurred: {str(e)} 🚨"
|
| 207 |
except Exception as e:
|