Luí*_*lva 11 node.js typescript nestjs prisma
这是我的 DTO
export class CreateGradeDto {
  id: string;
  first_bimester?: number;
  second_bimester?: number;
  third_bimester?: number;
  fourth_bimester?: number;
  studentId: never;
  classId: string;
  createdAt: Date;
  updatedAt: Date;
}
这是我的棱镜模型
model Grades {
  id              String   @id @unique
  first_bimester  Float
  second_bimester Float
  third_bimester  Float
  fourth_bimester Float
  student         Student  @relation(fields: [studentId], references: [id])
  studentId       String   @unique
  class           Classes  @relation(fields: [classId], references: [id])
  classId         String   @unique
  createdAt       DateTime @default(now())
  updatedAt       DateTime @default(now())
}
这是学生模型
export class CreateStudentDto {
  id: string;
  name: string;
  cpf: string;
  createdAt: Date;
  updatedAt: Date;
}
(我在这个项目中使用nestjs)
我不明白为什么我无法保存成绩,即使知道 id 一直保存为字符串。我还检查了真正的 postgre 数据库,它们被保存为字符串。
有人能帮我吗??
Tom*_*ara 16
我最初无法弄清楚如何创建记录并将其与现有记录关联。我遇到了和你一样的错误(好吧,number is not assignable to type never因为我的 ID 是数字)。
然后我在这里找到了文档的相关部分: https: //www.prisma.io/docs/concepts/components/prisma-client/relation-queries/#connect-an-existing-record - 它并不完全是前面和中心。您的情况的语法类似于:
prisma.grade.create({
    data: {
      student: { connect: { id: 'your-student-id' } },
      // ... rest of the grade data
    },
  });
因此,是的,您需要进行某种映射,将 DTO 中的关系字段转换为上面的语法。就像是:
// Create a copy of the dto data without the foreign key fields. Using lodash here for brevity
const gradeData = _.omit(createGradeDto, ['studentId', 'classId']);
prisma.grade.create({
    data: {
      ...gradeData,
      student: { connect: { id: createGradeDto.studentId } },
      class: { connect: { id: createGradeDto.classId } },
    },
  });
| 归档时间: | 
 | 
| 查看次数: | 20662 次 | 
| 最近记录: |