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.

Fix Empty Collection WHERE Filter Issue (#23086)

NeatGuyCoding47cc95184132
diff --git a/api/services/app_service.py b/api/services/app_service.py
index 3557f133376..0f22666d5a4 100644
--- a/api/services/app_service.py
+++ b/api/services/app_service.py
@@ -53,9 +53,10 @@ class AppService:
if args.get("name"):
name = args["name"][:30]
filters.append(App.name.ilike(f"%{name}%"))
- if args.get("tag_ids"):
+ # Check if tag_ids is not empty to avoid WHERE false condition
+ if args.get("tag_ids") and len(args["tag_ids"]) > 0:
target_ids = TagService.get_target_ids_by_tag_ids("app", tenant_id, args["tag_ids"])
- if target_ids:
+ if target_ids and len(target_ids) > 0:
filters.append(App.id.in_(target_ids))
else:
return None
diff --git a/api/services/conversation_service.py b/api/services/conversation_service.py
index 525c87fe4a7..206c832a201 100644
--- a/api/services/conversation_service.py
+++ b/api/services/conversation_service.py
@@ -46,9 +46,11 @@ class ConversationService:
Conversation.from_account_id == (user.id if isinstance(user, Account) else None),
or_(Conversation.invoke_from.is_(None), Conversation.invoke_from == invoke_from.value),
)
- if include_ids is not None:
+ # Check if include_ids is not None and not empty to avoid WHERE false condition
+ if include_ids is not None and len(include_ids) > 0:
stmt = stmt.where(Conversation.id.in_(include_ids))
- if exclude_ids is not None:
+ # Check if exclude_ids is not None and not empty to avoid WHERE false condition
+ if exclude_ids is not None and len(exclude_ids) > 0:
stmt = stmt.where(~Conversation.id.in_(exclude_ids))
# define sort fields and directions
diff --git a/api/services/dataset_service.py b/api/services/dataset_service.py
index 209d153b0c9..1280399990f 100644
--- a/api/services/dataset_service.py
+++ b/api/services/dataset_service.py
@@ -91,14 +91,16 @@ class DatasetService:
if user.current_role == TenantAccountRole.DATASET_OPERATOR:
# only show datasets that the user has permission to access
- if permitted_dataset_ids:
+ # Check if permitted_dataset_ids is not empty to avoid WHERE false condition
+ if permitted_dataset_ids and len(permitted_dataset_ids) > 0:
query = query.where(Dataset.id.in_(permitted_dataset_ids))
else:
return [], 0
else:
if user.current_role != TenantAccountRole.OWNER or not include_all:
# show all datasets that the user has permission to access
- if permitted_dataset_ids:
+ # Check if permitted_dataset_ids is not empty to avoid WHERE false condition
+ if permitted_dataset_ids and len(permitted_dataset_ids) > 0:
query = query.where(
db.or_(
Dataset.permission == DatasetPermissionEnum.ALL_TEAM,
@@ -127,9 +129,10 @@ class DatasetService:
if search:
query = query.where(Dataset.name.ilike(f"%{search}%"))
- if tag_ids:
+ # Check if tag_ids is not empty to avoid WHERE false condition
+ if tag_ids and len(tag_ids) > 0:
target_ids = TagService.get_target_ids_by_tag_ids("knowledge", tenant_id, tag_ids)
- if target_ids:
+ if target_ids and len(target_ids) > 0:
query = query.where(Dataset.id.in_(target_ids))
else:
return [], 0
@@ -158,6 +161,9 @@ class DatasetService:
@staticmethod
def get_datasets_by_ids(ids, tenant_id):
+ # Check if ids is not empty to avoid WHERE false condition
+ if not ids or len(ids) == 0:
+ return [], 0
stmt = select(Dataset).where(Dataset.id.in_(ids), Dataset.tenant_id == tenant_id)
datasets = db.paginate(select=stmt, page=1, per_page=len(ids), max_per_page=len(ids), error_out=False)
@@ -951,6 +957,9 @@ class DocumentService:
@staticmethod
def delete_documents(dataset: Dataset, document_ids: list[str]):
+ # Check if document_ids is not empty to avoid WHERE false condition
+ if not document_ids or len(document_ids) == 0:
+ return
documents = db.session.query(Document).where(Document.id.in_(document_ids)).all()
file_ids = [
document.data_source_info_dict["upload_file_id"]
@@ -2320,6 +2329,9 @@ class SegmentService:
@classmethod
def delete_segments(cls, segment_ids: list, document: Document, dataset: Dataset):
+ # Check if segment_ids is not empty to avoid WHERE false condition
+ if not segment_ids or len(segment_ids) == 0:
+ return
index_node_ids = (
db.session.query(DocumentSegment)
.with_entities(DocumentSegment.index_node_id)
@@ -2339,6 +2351,9 @@ class SegmentService:
@classmethod
def update_segments_status(cls, segment_ids: list, action: str, dataset: Dataset, document: Document):
+ # Check if segment_ids is not empty to avoid WHERE false condition
+ if not segment_ids or len(segment_ids) == 0:
+ return
if action == "enable":
segments = (
db.session.query(DocumentSegment)
@@ -2600,7 +2615,8 @@ class SegmentService:
DocumentSegment.document_id == document_id, DocumentSegment.tenant_id == tenant_id
)
- if status_list:
+ # Check if status_list is not empty to avoid WHERE false condition
+ if status_list and len(status_list) > 0:
query = query.where(DocumentSegment.status.in_(status_list))
if keyword:
diff --git a/api/services/message_service.py b/api/services/message_service.py
index 283b7b9b4b9..a19d6ee1575 100644
--- a/api/services/message_service.py
+++ b/api/services/message_service.py
@@ -111,7 +111,8 @@ class MessageService:
base_query = base_query.where(Message.conversation_id == conversation.id)
- if include_ids is not None:
+ # Check if include_ids is not None and not empty to avoid WHERE false condition
+ if include_ids is not None and len(include_ids) > 0:
base_query = base_query.where(Message.id.in_(include_ids))
if last_id:
diff --git a/api/services/tag_service.py b/api/services/tag_service.py
index 75fa52a75ce..2e5e96214bc 100644
--- a/api/services/tag_service.py
+++ b/api/services/tag_service.py
@@ -26,6 +26,9 @@ class TagService:
@staticmethod
def get_target_ids_by_tag_ids(tag_type: str, current_tenant_id: str, tag_ids: list) -> list:
+ # Check if tag_ids is not empty to avoid WHERE false condition
+ if not tag_ids or len(tag_ids) == 0:
+ return []
tags = (
db.session.query(Tag)
.where(Tag.id.in_(tag_ids), Tag.tenant_id == current_tenant_id, Tag.type == tag_type)
@@ -34,6 +37,9 @@ class TagService:
if not tags:
return []
tag_ids = [tag.id for tag in tags]
+ # Check if tag_ids is not empty to avoid WHERE false condition
+ if not tag_ids or len(tag_ids) == 0:
+ return []
tag_bindings = (
db.session.query(TagBinding.target_id)
.where(TagBinding.tag_id.in_(tag_ids), TagBinding.tenant_id == current_tenant_id)