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.

add secondary sort_key when using `order_by` and `paginate` at the same time (#7225)

feng02fe2e350ce55
diff --git a/api/controllers/console/datasets/datasets_document.py b/api/controllers/console/datasets/datasets_document.py
index afe0ca7c69b..976b97660ae 100644
--- a/api/controllers/console/datasets/datasets_document.py
+++ b/api/controllers/console/datasets/datasets_document.py
@@ -178,11 +178,20 @@ class DatasetDocumentListApi(Resource):
.subquery()
query = query.outerjoin(sub_query, sub_query.c.document_id == Document.id) \
- .order_by(sort_logic(db.func.coalesce(sub_query.c.total_hit_count, 0)))
+ .order_by(
+ sort_logic(db.func.coalesce(sub_query.c.total_hit_count, 0)),
+ sort_logic(Document.position),
+ )
elif sort == 'created_at':
- query = query.order_by(sort_logic(Document.created_at))
+ query = query.order_by(
+ sort_logic(Document.created_at),
+ sort_logic(Document.position),
+ )
else:
- query = query.order_by(desc(Document.created_at))
+ query = query.order_by(
+ desc(Document.created_at),
+ desc(Document.position),
+ )
paginated_documents = query.paginate(
page=page, per_page=limit, max_per_page=100, error_out=False)