Hey team! I am having troubles updating/creating a...
# orm-help
d
Hey team! I am having troubles updating/creating an optional relation:
Copy code
Evaluations: {
                        upsert: [
                            {
                                where: {
                                    topic_id_assessment_record_id_unique: {
                                        topicId: topicId as string,
                                        assessmentRecordId: recordId as string,
                                    },
                                },
                                create: {
                                    User: {
                                        connect: {
                                            id: user.id,
                                        },
                                    },
                                    Topic: {
                                        connect: {
                                            id: topicId as string,
                                        },
                                    },
                                    Level: {
                                        connect: {
                                            id: levelId as string,
                                        },
                                    },
                                    Stage: {
                                        connect: {
                                            id: stageId as string,
                                        },
                                    },
                                    comment: comment as string,
                                    score: 321,
                                },
                                update: {
                                    levelId: levelId as string,
                                    stageId: stageId as string,
                                },
Copy code
model Evaluation {
  id                 String   @id @default(cuid())
  assessmentRecordId String
  userId             String
  topicId            String
  levelId            String?
  stageId            String?
  score              Float
  comment            String?
  createdAt          DateTime @default(now())
  updatedAt          DateTime @updatedAt @map(name: "updated_at")

  Record AssessmentRecord @relation(fields: [assessmentRecordId], references: [id])
  User   User             @relation(fields: [userId], references: [id])
  Topic  Topic            @relation(fields: [topicId], references: [id])
  Level  Level?           @relation(fields: [levelId], references: [id])
  Stage  Stage?           @relation(fields: [stageId], references: [id])

  @@unique([topicId, assessmentRecordId], name: "topic_id_assessment_record_id_unique")
  @@map(name: "evaluations")
}
Both
null
and
undefined
as arguments throw error 😕
so the question is - am I doing something wrong with the schema/upsert function or I can't create Evaluation without linking to Level & Stage?
Here is an error if it helps:
Copy code
Invalid `prisma.assessmentRecord.update()` invocation:

{
  where: {
    id: 'ckqt3vtqa0327m1yb95ncr72i'
  },
  data: {
    Evaluations: {
      upsert: {
        '0': {
          where: {
            topic_id_assessment_record_id_unique: {
              topicId: 'ckqt3p1d90394njyb3j8fbu8g',
              assessmentRecordId: 'ckqt3vtqa0327m1yb95ncr72i'
            }
          },
          create: {
            User: {
              connect: {
                id: 'ckqt3uchl0008m1yb16ytyiwa'
              }
            },
            Topic: {
              connect: {
                id: 'ckqt3p1d90394njyb3j8fbu8g'
              }
            },
            Level: {
              connect: {
?               id?: String
              },
?             create?: LevelCreateWithoutEvaluationsInput | LevelUncheckedCreateWithoutEvaluationsInput,
?             connectOrCreate?: {
?               where: LevelWhereUniqueInput,
?               create: LevelCreateWithoutEvaluationsInput | LevelUncheckedCreateWithoutEvaluationsInput
?             }
            },
            Stage: {
              connect: {
?               id?: String
              },
?             create?: StageCreateWithoutEvaluationsInput | StageUncheckedCreateWithoutEvaluationsInput,
?             connectOrCreate?: {
?               where: StageWhereUniqueInput,
?               create: StageCreateWithoutEvaluationsInput | StageUncheckedCreateWithoutEvaluationsInput
?             }
            },
            comment: undefined,
            score: 321
          },
          update: {
            levelId: undefined,
            stageId: undefined
          }
        }
      }
    }
  },
  include: {
    Evaluations: true
  }
}

Argument data.Evaluations.upsert.0.create.Level.connect of type LevelWhereUniqueInput needs at least one argument. Available args are listed in green.
Argument data.Evaluations.upsert.0.create.Stage.connect of type StageWhereUniqueInput needs at least one argument. Available args are listed in green.
r
If the argument is
undefined
or
null
then you need to omit the relation entirely. So a condition before the relation would be the way to go. Something like:
Copy code
...(user.id && {
  User: { connect: { id: user.id } }
})
d
thanks a lot!
👍 1