|
22 | 22 | from core.rag.index_processor.index_processor_base import BaseIndexProcessor, SummaryIndexSettingDict |
23 | 23 | from core.rag.models.document import AttachmentDocument, ChildDocument, Document, ParentChildStructureChunk |
24 | 24 | from core.rag.retrieval.retrieval_methods import RetrievalMethod |
| 25 | +from sqlalchemy import delete, select |
| 26 | + |
25 | 27 | from extensions.ext_database import db |
26 | 28 | from libs import helper |
27 | 29 | from models import Account |
@@ -177,36 +179,39 @@ def clean(self, dataset: Dataset, node_ids: list[str] | None, with_keywords: boo |
177 | 179 | child_node_ids = precomputed_child_node_ids |
178 | 180 | else: |
179 | 181 | # Fallback to original query (may fail if segments are already deleted) |
180 | | - child_node_ids = ( |
181 | | - db.session.query(ChildChunk.index_node_id) |
| 182 | + rows = db.session.execute( |
| 183 | + select(ChildChunk.index_node_id) |
182 | 184 | .join(DocumentSegment, ChildChunk.segment_id == DocumentSegment.id) |
183 | 185 | .where( |
184 | 186 | DocumentSegment.dataset_id == dataset.id, |
185 | 187 | DocumentSegment.index_node_id.in_(node_ids), |
186 | 188 | ChildChunk.dataset_id == dataset.id, |
187 | 189 | ) |
188 | | - .all() |
189 | | - ) |
190 | | - child_node_ids = [child_node_id[0] for child_node_id in child_node_ids if child_node_id[0]] |
| 190 | + ).all() |
| 191 | + child_node_ids = [row[0] for row in rows if row[0]] |
191 | 192 |
|
192 | 193 | # Delete from vector index |
193 | 194 | if child_node_ids: |
194 | 195 | vector.delete_by_ids(child_node_ids) |
195 | 196 |
|
196 | 197 | # Delete from database |
197 | 198 | if delete_child_chunks and child_node_ids: |
198 | | - db.session.query(ChildChunk).where( |
199 | | - ChildChunk.dataset_id == dataset.id, ChildChunk.index_node_id.in_(child_node_ids) |
200 | | - ).delete(synchronize_session=False) |
| 199 | + db.session.execute( |
| 200 | + delete(ChildChunk).where( |
| 201 | + ChildChunk.dataset_id == dataset.id, ChildChunk.index_node_id.in_(child_node_ids) |
| 202 | + ) |
| 203 | + ) |
201 | 204 | db.session.commit() |
202 | 205 | else: |
203 | 206 | vector.delete() |
204 | 207 |
|
205 | 208 | if delete_child_chunks: |
206 | 209 | # Use existing compound index: (tenant_id, dataset_id, ...) |
207 | | - db.session.query(ChildChunk).where( |
208 | | - ChildChunk.tenant_id == dataset.tenant_id, ChildChunk.dataset_id == dataset.id |
209 | | - ).delete(synchronize_session=False) |
| 210 | + db.session.execute( |
| 211 | + delete(ChildChunk).where( |
| 212 | + ChildChunk.tenant_id == dataset.tenant_id, ChildChunk.dataset_id == dataset.id |
| 213 | + ) |
| 214 | + ) |
210 | 215 | db.session.commit() |
211 | 216 |
|
212 | 217 | def retrieve( |
|
0 commit comments