Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

refactor(models): pass session into ChildChunk accessors (#41370)

Keithc8606db16b37
diff --git a/api/models/dataset.py b/api/models/dataset.py
index ae54b5a8433..891d949c8e6 100644
--- a/api/models/dataset.py
+++ b/api/models/dataset.py
@@ -1173,17 +1173,14 @@ class ChildChunk(TypeBase):
)
error: Mapped[str | None] = mapped_column(LongText, nullable=True, init=False)
- @property
- def dataset(self):
- return db.session.scalar(select(Dataset).where(Dataset.id == self.dataset_id))
+ def dataset(self, session: Session) -> Dataset | None:
+ return session.scalar(select(Dataset).where(Dataset.id == self.dataset_id))
- @property
- def document(self):
- return db.session.scalar(select(Document).where(Document.id == self.document_id))
+ def document(self, session: Session) -> Document | None:
+ return session.scalar(select(Document).where(Document.id == self.document_id))
- @property
- def segment(self):
- return db.session.scalar(select(DocumentSegment).where(DocumentSegment.id == self.segment_id))
+ def segment(self, session: Session) -> DocumentSegment | None:
+ return session.scalar(select(DocumentSegment).where(DocumentSegment.id == self.segment_id))
class AppDatasetJoin(TypeBase):
diff --git a/api/tests/unit_tests/models/test_dataset_models.py b/api/tests/unit_tests/models/test_dataset_models.py
index ff634dcdc50..e724b6e0e86 100644
--- a/api/tests/unit_tests/models/test_dataset_models.py
+++ b/api/tests/unit_tests/models/test_dataset_models.py
@@ -1611,3 +1611,48 @@ class TestModelIntegration:
assert result["indexing_status"] == IndexingStatus.COMPLETED
assert result["segment_count"] == 5
assert result["hit_count"] == 10
+
+
+class TestChildChunkSessionAccessors:
+ """Regression coverage for the ``ChildChunk`` accessors refactored to take a caller-provided session.
+
+ ``ChildChunk.dataset`` / ``document`` / ``segment`` were ``@property`` accessors reaching for the
+ global ``db.session`` internally; they are now plain methods accepting a ``Session`` explicitly.
+ """
+
+ def test_accessors_resolve_related_rows_via_caller_session(self, sqlite_session: Session):
+ dataset = _make_dataset(dataset_id=str(uuid4()), tenant_id=str(uuid4()))
+ document = _make_document(document_id=str(uuid4()), dataset_id=dataset.id, tenant_id=dataset.tenant_id)
+ segment = _make_segments(document, [0])[0]
+ child_chunk = ChildChunk(
+ tenant_id=segment.tenant_id,
+ dataset_id=segment.dataset_id,
+ document_id=segment.document_id,
+ segment_id=segment.id,
+ position=1,
+ content="chunk",
+ word_count=1,
+ created_by="account-1",
+ )
+ sqlite_session.add_all([dataset, document, segment, child_chunk])
+ sqlite_session.flush()
+
+ assert child_chunk.dataset(session=sqlite_session) is dataset
+ assert child_chunk.document(session=sqlite_session) is document
+ assert child_chunk.segment(session=sqlite_session) is segment
+
+ def test_accessors_return_none_when_related_rows_are_absent(self, sqlite_session: Session):
+ child_chunk = ChildChunk(
+ tenant_id=str(uuid4()),
+ dataset_id=str(uuid4()),
+ document_id=str(uuid4()),
+ segment_id=str(uuid4()),
+ position=1,
+ content="chunk",
+ word_count=1,
+ created_by="account-1",
+ )
+
+ assert child_chunk.dataset(session=sqlite_session) is None
+ assert child_chunk.document(session=sqlite_session) is None
+ assert child_chunk.segment(session=sqlite_session) is None