Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -176,25 +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 |
-
# First try to
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
192 |
|
193 |
-
# Now delete with the correct partition key
|
194 |
-
partition_key = doc.get('id', id) # Default to id if no partition key found
|
195 |
-
container.delete_item(item=id, partition_key=partition_key)
|
196 |
return True, f"Successfully deleted record with id: {id} 🗑️"
|
197 |
|
|
|
|
|
198 |
except exceptions.CosmosHttpResponseError as e:
|
199 |
return False, f"HTTP error occurred: {str(e)} 🚨"
|
200 |
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 try to find the document using a query
|
180 |
+
query = f"SELECT * FROM c WHERE c.id = @id"
|
181 |
+
params = [{"name": "@id", "value": id}]
|
182 |
+
items = list(container.query_items(
|
183 |
+
query=query,
|
184 |
+
parameters=params,
|
185 |
+
enable_cross_partition_query=True
|
186 |
+
))
|
187 |
+
|
188 |
+
if not items:
|
189 |
+
return False, f"Record with id {id} not found. 🕵️♂️"
|
190 |
+
|
191 |
+
doc = items[0]
|
192 |
+
partition_key = doc.get('id') # Using id as partition key
|
193 |
+
|
194 |
+
# Delete with explicit partition key
|
195 |
+
container.delete_item(
|
196 |
+
item=id,
|
197 |
+
partition_key=partition_key,
|
198 |
+
enable_cross_partition_query=True
|
199 |
+
)
|
200 |
|
|
|
|
|
|
|
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 or already deleted. 🕵️♂️"
|
205 |
except exceptions.CosmosHttpResponseError as e:
|
206 |
return False, f"HTTP error occurred: {str(e)} 🚨"
|
207 |
except Exception as e:
|