Prisma:类型'字符串'不可分配给类型'从不' [英] prisma: Type 'string' is not assignable to type 'never'

查看:0
本文介绍了Prisma:类型'字符串'不可分配给类型'从不'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

THIS IS THE ACTUAL ERROR

这是我的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;
}

这是我的Prisma模型

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数据库,它们被保存为字符串。

有人能帮帮我吗??

推荐答案

我最初无法弄清楚如何创建记录并将其与现有记录相关联。我收到了与您相同的错误(好吧,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 } },
    },
  });

这篇关于Prisma:类型'字符串'不可分配给类型'从不'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆