fix(server): 档案子记录编辑修复 ER_BAD_FIELD_ERROR

findOne+select+eager join 时 TypeORM 0.3.x 生成缺 id 列的 distinctAlias 子查询
(Unknown column 'distinctAlias.xxx_id'),所有档案子记录编辑/删除均 500。
改回全列读取(单行主键查询成本可忽略)
This commit is contained in:
2026-08-11 15:18:12 +08:00
parent 2499f90231
commit 3e3c05b8bc

View File

@@ -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<number | null> {
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: