THIS IS THE ACTUAL ERROR
This is my 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;
}
This is my prisma model
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())
}
And this is the student model
export class CreateStudentDto {
id: string;
name: string;
cpf: string;
createdAt: Date;
updatedAt: Date;
}
(I'm using nestjs for this project)
i can't figure out why i can't save the grades, even knowing that all the time the id's are saved as string. I also checked the real postgre database and they are saved as strings.
Can someone help me??
I couldn't initially figure out how to create a record and relate it to an existing record. I got the same error as you (well, number is not assignable to type never because my IDs are numeric).
Then I found the relevant part of the docs here: https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries/#connect-an-existing-record - it's not entirely front and centre. The syntax in your case would be something like:
prisma.grade.create({
data: {
student: { connect: { id: 'your-student-id' } },
// ... rest of the grade data
},
});
So, yes, you will need to do some sort of mapping to convert the relation fields in the DTO to the syntax above. Something like:
// 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 } },
},
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With