awacke1 commited on
Commit
b24908a
·
verified ·
1 Parent(s): 98c97fa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -24
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
- # Query to find the document
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 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: