Save related models using Prisma

122 Views Asked by At

I have two models, Quiz and QuizQuestions a One to Many relation, i recive both on the request and want to save them at once since they are related to each other. But i'm receiving Error on create

schema:

   model Quiz {
        quiz_id          Int             @id @default(autoincrement())
        quiz_description String          @db.VarChar(200)
        quiz_status      Int?            @default(1) @db.Integer
        questions        QuizQuestions[]

        @@map("quiz")
    }

    model QuizQuestions {
        question_id          Int     @id @default(autoincrement())
        id_quiz              Int     @db.Integer
        question_description String  @db.VarChar(250)
        question_type        Int     @db.Integer
        question_required    Boolean @db.Boolean
        quiz                 Quiz    @relation(fields: [id_quiz], references: [quiz_id])

        @@map("questions")
    }

try to create -->

controller:

@Post()
    public async saveNewQuiz(
        @Body() quiz: Prisma.QuizCreateInput
    ) {
        const result = await this.quizServiceDB.saveNewQuiz(quiz);
        return ResponseRest.buildSuccess(result);
    }

QuizCreateInput type auto generated by Prisma:

  export type QuizCreateInput = {
    quiz_description: string
    quiz_status?:     number | null
    questions?:       QuizQuestionsCreateNestedManyWithoutQuizInput
  }

service:

        public async saveNewQuiz(quiz: Prisma.QuizCreateInput) {
        const quizRepository = this.prisma.quiz;

        const quizSaved = await quizRepository.create({ data: quiz });

        return { quiz_id: quizSaved.quiz_id };
        }

Error given:

Invalid `prisma.quiz.create()` invocation: { data: { quiz_description: 'test', questions: [ { question_description: 'test', question_type: 2, question_required: false } ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } } Argument questions: Got invalid value [ { question_description: 'test', question_type: 2, question_required: false } ] on prisma.createOneQuiz. 

Provided List, expected QuizQuestionsCreateNestedManyWithoutQuestionInput: type QuizQuestionsCreateNestedManyWithoutQuizInput { create?: 
QuizQuestionsCreateWithoutQuizInput | List | 
QuizQuestionsUncheckedCreateWithoutQuizInput | List connectOrCreate?: QuizQuestionsCreateOrConnectWithoutQuizInput | List createMany?: QuizQuestionsCreateManyQuizInputEnvelope connect?: 
QuizQuestionsWhereUniqueInput | List }
0

There are 0 best solutions below