Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -170,20 +170,33 @@ def update_record(container, updated_record):
|
|
170 |
except Exception as e:
|
171 |
return False, f"Error: {traceback.format_exc()} ๐ฑ"
|
172 |
|
173 |
-
# ๐๏ธ Delete record โ
|
174 |
def delete_record(container, record):
|
175 |
try:
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
except exceptions.CosmosHttpResponseError as e:
|
182 |
return False, f"HTTP error: {str(e)} ๐จ"
|
183 |
except Exception as e:
|
184 |
return False, f"Unexpected error: {traceback.format_exc()} ๐ฑ"
|
185 |
|
186 |
|
|
|
187 |
# ๐พ Save a new document to Cosmos DB with extra fields
|
188 |
def save_to_cosmos_db(container, query, response1, response2):
|
189 |
try:
|
|
|
170 |
except Exception as e:
|
171 |
return False, f"Error: {traceback.format_exc()} ๐ฑ"
|
172 |
|
173 |
+
# ๐๏ธ Delete record โ query first, then delete using the actual partition key
|
174 |
def delete_record(container, record):
|
175 |
try:
|
176 |
+
# Query for the document by id to get the actual partition key value.
|
177 |
+
query = "SELECT * FROM c WHERE c.id=@id"
|
178 |
+
parameters = [{"name": "@id", "value": record["id"]}]
|
179 |
+
docs = list(container.query_items(
|
180 |
+
query=query,
|
181 |
+
parameters=parameters,
|
182 |
+
enable_cross_partition_query=True
|
183 |
+
))
|
184 |
+
if not docs:
|
185 |
+
# Document doesn't exist; treat as already deleted.
|
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 |
+
partition_key = doc.get("name") # assuming 'name' is your partition key
|
191 |
+
container.delete_item(item=doc["id"], partition_key=partition_key)
|
192 |
+
return True, f"Record {doc['id']} successfully deleted. ๐๏ธ"
|
193 |
except exceptions.CosmosHttpResponseError as e:
|
194 |
return False, f"HTTP error: {str(e)} ๐จ"
|
195 |
except Exception as e:
|
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:
|