Prismajs, добавлять одинаковые записи в базу данных

Рейтинг: 0Ответов: 0Опубликовано: 18.08.2023

У меня есть 2 модели

model Post {
  id          Int          @id @default(autoincrement())
  title       String?
  text        String?
  countLikes  Boolean      @default(false)
  countComent Boolean      @default(false)
  countWatch  Int          @default(0)
  imgAvatar   String?
  user        User         @relation(fields: [userId], references: [id])
  userId      Int
  delete      Boolean      @default(false)
  likes       LikePost[]
  coment      ComentPost[]
}

model ComentPost {
  id     Int     @id @default(autoincrement())
  user   User   @relation(fields: [userId], references: [id])
  userId Int
  post   Post   @relation(fields: [postId], references: [id])
  postId Int
  text   String?
  delete Boolean @default(false)
}

С помощью запроса,я создаю коментарий с userId и postId

  async setcoment(userId, postId, text) {
    let newComent = await this.prisma.comentPost.create({
      data: {
          userId:userId,
          postId:postId,
          text:text
      },
    });
    return newComent
  }

Запись добавляется,все отличное. но если я опять захочу добавить коментарий, то уже не получится, выдает ошибку

let newComent = await this.prisma.comentPost.create(
Unique constraint failed on the constraint: `ComentPost_userId_key` 

Хотелось бы иметь записи если userId или postId уже есть.

Ответы

Ответов пока нет.