fix: 搜索 tag 过滤改用 cast(ARRAY(Uuid)),杜绝 asyncpg 把 ARRAY[$n] 推断成 text[] 导致 uuid[] && text[] 500(根因在 year_counts 子查询复用 conditions)
CI / backend (push) Waiting to run
CI / frontend (push) Waiting to run

This commit is contained in:
34047007@qq.com
2026-08-10 17:49:57 +08:00
parent 3d05f52716
commit 7f95084d76
+6 -4
View File
@@ -4,7 +4,7 @@ import logging
import re
from datetime import datetime
from sqlalchemy import String, and_, case, cast, exists, func, literal_column, not_, or_, select, text
from sqlalchemy import ARRAY, String, Uuid, and_, case, cast, exists, func, literal_column, not_, or_, select, text
logger = logging.getLogger(__name__)
from sqlalchemy.ext.asyncio import AsyncSession
@@ -506,9 +506,11 @@ class AdvancedSearchEngine:
child_conds = [GlobalTag.path.like(f"{p}%") for p in paths]
children = (await db.execute(select(GlobalTag.id).where(or_(*child_conds)))).scalars().all()
all_tag_ids.update(children)
uids = list(all_tag_ids)
from sqlalchemy.dialects.postgresql import array as _pg_array, UUID as _pg_uuid
conditions.append(GlobalLiterature.tag_ids.overlap(_pg_array(uids, type_=_pg_uuid())))
uids = [_uuid.UUID(u) if isinstance(u, str) else u for u in all_tag_ids]
# 显式 cast 到 ARRAY(Uuid):不依赖 _pg_array 的参数类型推断(asyncpg prepared
# statement 会把 ARRAY[$n] 推断成 text[],导致 uuid[] && text[] 500)。cast 后
# 编译产物必带 ::uuid[],杜绝推断漂移。
conditions.append(GlobalLiterature.tag_ids.overlap(cast(uids, ARRAY(Uuid))))
# 发表类型(PG JSONB contains
if pub_types: