From 3e3c05b8bc88853ad34f262572040eb77939e084 Mon Sep 17 00:00:00 2001 From: wangziqi Date: Tue, 11 Aug 2026 15:18:12 +0800 Subject: [PATCH] =?UTF-8?q?fix(server):=20=E6=A1=A3=E6=A1=88=E5=AD=90?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E7=BC=96=E8=BE=91=E4=BF=AE=E5=A4=8D=20ER=5FB?= =?UTF-8?q?AD=5FFIELD=5FERROR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit findOne+select+eager join 时 TypeORM 0.3.x 生成缺 id 列的 distinctAlias 子查询 (Unknown column 'distinctAlias.xxx_id'),所有档案子记录编辑/删除均 500。 改回全列读取(单行主键查询成本可忽略) --- apps/server/src/archive/archive.service.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/apps/server/src/archive/archive.service.ts b/apps/server/src/archive/archive.service.ts index bca07007..92cae657 100644 --- a/apps/server/src/archive/archive.service.ts +++ b/apps/server/src/archive/archive.service.ts @@ -65,6 +65,9 @@ export class ArchiveService { /** * 解析某条档案子记录属于哪个学生(用于按学生范围做 IDOR 校验)。 * 找不到返回 null。 + * 注意:不传 select —— 这些实体带 eager 的 student 关系,TypeORM 0.3.x 对 + * findOne+select+eager join 会生成缺 id 列的 distinctAlias 子查询而报 + * ER_BAD_FIELD_ERROR(单行主键查询,全列读取成本可忽略)。 */ async resolveRecordStudentId( kind: 'enrollment' | 'examScore' | 'learningRecord' | 'attachment', @@ -72,19 +75,19 @@ export class ArchiveService { ): Promise { switch (kind) { case 'enrollment': { - const row = await this.enrollmentRepo.findOne({ where: { id }, select: ['studentId'] }); + const row = await this.enrollmentRepo.findOne({ where: { id } }); return row?.studentId ?? null; } case 'examScore': { - const row = await this.examScoreRepo.findOne({ where: { id }, select: ['studentId'] }); + const row = await this.examScoreRepo.findOne({ where: { id } }); return row?.studentId ?? null; } case 'learningRecord': { - const row = await this.learningRecordRepo.findOne({ where: { id }, select: ['studentId'] }); + const row = await this.learningRecordRepo.findOne({ where: { id } }); return row?.studentId ?? null; } case 'attachment': { - const row = await this.attachmentRepo.findOne({ where: { id }, select: ['studentId'] }); + const row = await this.attachmentRepo.findOne({ where: { id } }); return row?.studentId ?? null; } default: