#Explicit many-to-many relations - Update query

4 messages · Page 1 of 1 (latest)

thin wolf
#

Hello community,
I want help with explicit many-to-many relations in MySQL.
I have Topic and Question model both have m-n relation to each other. Third model QuestionsOnTopics is the link between the two. Following is the schema.

model Topic {
  id        Int                 @id @default(autoincrement())
  questions       QuestionsOnTopics[]
}

model Question {
  id            Int               @id @default(autoincrement())
  topics        QuestionsOnTopics[]
}

model QuestionsOnTopics {
  topic     Topic    @relation(fields: [topicId], references: [id])
  topicId   Int
  question       Question      @relation(fields: [questionId], references: [id])
  questionId     Int

  @@id([topicId, seqId])
  @@index([topicId])
  @@index([seqId])
}

If I want to update the topics of a question, what is the right way to do it?
I am using the following method, but getting error: Argument whereof type QuestionsOnTopicsWhereUniqueInput needs at least one oftopicId_questionId arguments. Available options are marked with ?

Following is my query:

await db.question.update({
      where: {
        id: questionId,
      },
      data: {
        topics: {
          connectOrCreate: topicIdArray.map((id: number) => ({
            where: {
              topicId: id,
            },
            create: {
              topicId: id,
            },
          })),
        },
        ...values,
      },
    })
cold herald
#

Update expects you to update a single row, and for that it forces you to provide all fields that compose the primary key

#

Use updateMany when you are providing partially the PK

thin wolf