Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -176,31 +176,28 @@ 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 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
190 |
|
191 |
-
doc = items[0]
|
192 |
-
partition_key = doc.get('id') # Using id as partition key
|
193 |
-
|
194 |
-
# Delete with partition key
|
195 |
-
container.delete_item(
|
196 |
-
item=id,
|
197 |
-
partition_key=partition_key
|
198 |
-
)
|
199 |
-
|
200 |
-
return True, f"Successfully deleted record with id: {id} 🗑️"
|
201 |
-
|
202 |
-
except exceptions.CosmosResourceNotFoundError:
|
203 |
-
return False, f"Record with id {id} not found or already deleted. 🕵️♂️"
|
204 |
except exceptions.CosmosHttpResponseError as e:
|
205 |
return False, f"HTTP error occurred: {str(e)} 🚨"
|
206 |
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 |
+
# Direct read attempt first
|
180 |
+
try:
|
181 |
+
doc = container.read_item(item=id, partition_key=id)
|
182 |
+
container.delete_item(item=id, partition_key=id)
|
183 |
+
return True, f"Successfully deleted record with id: {id} 🗑️"
|
184 |
+
except exceptions.CosmosResourceNotFoundError:
|
185 |
+
# Fall back to cross-partition query
|
186 |
+
query = "SELECT * FROM c WHERE c.id = @id"
|
187 |
+
params = [{"name": "@id", "value": id}]
|
188 |
+
items = list(container.query_items(
|
189 |
+
query=query,
|
190 |
+
parameters=params,
|
191 |
+
enable_cross_partition_query=True
|
192 |
+
))
|
193 |
+
|
194 |
+
if not items:
|
195 |
+
return False, f"Record with id {id} not found. 🕵️♂️"
|
196 |
+
|
197 |
+
doc = items[0]
|
198 |
+
container.delete_item(item=id, partition_key=doc['id'])
|
199 |
+
return True, f"Successfully deleted record with id: {id} 🗑️"
|
200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
except exceptions.CosmosHttpResponseError as e:
|
202 |
return False, f"HTTP error occurred: {str(e)} 🚨"
|
203 |
except Exception as e:
|