I am having several problems when I try to create a new user with a nested related table. My schema is like this:
model User {
id Int @id @default(autoincrement())
username String @unique
email String @unique
avatar String @default("none")
role Role @default(USER)
password String
maxScore Int @default(0)
UserCategoryStats UserCategoryStats[]
}
model UserCategoryStats {
id Int @id @default(autoincrement())
user User @relation(fields: [userId], references: [id])
userId Int
category Category @relation(fields: [categoryId], references: [id])
categoryId Int
correctAnswers Int @default(0)
incorrectAnswers Int @default(0)
totalAnswers Int @default(0)
timesChosen Int @default(0)
}
And I am just trying to create an user with one(or more) UserCategoryStats like the docs say Like this:
const newUser = await prisma.user.create({
data: {
username,
email,
password: hashedPassword,
UserCategoryStats: {
create: { //Here is the error
correctAnswers: 0,
incorrectAnswers: 0,
totalAnswers: 0,
timesChosen: 0,
},
},
},
include: {
UserCategoryStats: true,
},
});
This is giving me this Typescript error :
Type '{ correctAnswers: number; incorrectAnswers: number; totalAnswers: number; timesChosen: number; }' is not assignable to type '(Without<UserCategoryStatsCreateWithoutUserInput, UserCategoryStatsUncheckedCreateWithoutUserInput> & UserCategoryStatsUncheckedCreateWithoutUserInput) | ... 5 more ... | undefined'. Type '{ correctAnswers: number; incorrectAnswers: number; totalAnswers: number; timesChosen: number; }' is not assignable to type 'undefined'
I have tried with multiple values but I simply dont know what is wrong. I am not able to create nested properties.
What am I doing wrong?
