fix(knowledge): drop COLLATE utf8mb4_unicode_ci from knowledge-square join

DM8 rejects ``COLLATE utf8mb4_unicode_ci`` (-2007 Syntax error)
because the collation name does not exist outside MySQL. The clause
was patching an "Illegal mix of collations" error on MySQL, but
collation belongs to the schema definition (table charset/collate),
not the query. Business code should not have to specify it inline.

Drop the COLLATE wrapper entirely; keep the CAST since INT
``knowledge.id`` still has to be compared against VARCHAR
``space_channel_member.business_id``. MySQL deployments need the
table-level collate matched at table-creation time, which they
already do via ``mysql_collate='utf8mb4_unicode_ci'`` table args
on the joined models.
This commit is contained in:
GuoQing Zhang
2026-05-28 15:13:54 +08:00
parent 7a398d7e9e
commit cb44c34ad6
@@ -3,7 +3,7 @@ from enum import Enum
from typing import Any, List, Optional, Tuple, Union, Dict
from pydantic import BaseModel, field_validator
from sqlalchemy import Boolean, Integer, String, collate
from sqlalchemy import Boolean, Integer, String
from sqlmodel import Column, DateTime, Field, case, delete, func, or_, select, text, update
from sqlmodel.sql.expression import Select, SelectOfScalar, col
@@ -767,6 +767,8 @@ class KnowledgeDao(KnowledgeBase):
rejection_cutoff = datetime.now() - REJECTED_STATUS_DISPLAY_WINDOW
kid_str = col(Knowledge.id).cast(String)
# Subquery: count subscribers (status=ACTIVE) per space
subscriber_subq = (
select(
@@ -791,13 +793,13 @@ class KnowledgeDao(KnowledgeBase):
)
.outerjoin(
SpaceChannelMember,
(collate(col(Knowledge.id).cast(String), 'utf8mb4_unicode_ci') == SpaceChannelMember.business_id)
(kid_str == SpaceChannelMember.business_id)
& (SpaceChannelMember.business_type == BusinessTypeEnum.SPACE)
& (SpaceChannelMember.user_id == user_id),
)
.outerjoin(
subscriber_subq,
collate(col(Knowledge.id).cast(String), 'utf8mb4_unicode_ci') == subscriber_subq.c.business_id,
kid_str == subscriber_subq.c.business_id,
)
.where(
Knowledge.type == KnowledgeTypeEnum.SPACE.value,