簡體   English   中英

Pisma 唯一約束失敗

[英]Pisma Unique constraint failed

你好(我是法國人,抱歉我的英語不好!!)

我想為一個具有 prisma 模式的項目創建數據庫,其中有一個用戶表,另一個用於用戶寫的評論,最后一個用於喜歡(每個用戶都可以喜歡每個評論)。

所以我的架構是:


model User {
  idUser     Int       @id @default(autoincrement())
  pseudo     String    @unique @db.VarChar(100)
  password   String    @db.VarChar(255)
  role       Role      @default(USER)
  nom        String    @db.VarChar(100)
  prenom     String    @db.VarChar(100)
  email      String    @unique @db.VarChar(100)
  info_email String    @default("null") @db.VarChar(200)

  writtenComments Comment[]
  likes Like [] 

  @@unique([idUser, pseudo])
}

model Comment {
  idComment         Int     @id @default(autoincrement())
  title      String  @db.VarChar(100)
  comment      String  @db.VarChar(200)

  author     User    @relation(fields: [authorId, authorPseudo], references: [idUser, pseudo], onDelete: Cascade)
  authorId   Int
  authorPseudo String

  likes Like [] 

  @@unique([idComment, title, authorPseudo, authorId])
}

model Like {
  idLikes         Int     @id @default(autoincrement())

  Liker     User    @relation( fields: [LikerId, LikerPseudo], references: [idUser, pseudo], onDelete: Cascade)
  LikerId   Int
  LikerPseudo String

  Commented     Comment    @relation(fields: [commentId, commentTitle, commentAuthor, commentAuthorId ], references: [idComment, title, authorPseudo,authorId], onDelete: Cascade)
  commentId   Int
  commentTitle String
  commentAuthor String
  commentAuthorId Int

  @@unique([commentId, LikerId])
}

enum Role {
  USER
  ADMIN
}

問題:like 是一個評論和一個用戶的組合,但是唯一約束失敗了。 在我的數據庫中,對於同一用戶喜歡的評論,我可以有兩個 ID。因此,一個用戶可以隨時喜歡他想要相同的評論。 當我在 Like 表中添加唯一約束時:出現失敗消息。 :/

Error: P2002

Unique constraint failed on the constraint: `Like_commentId_LikerId_key

謝謝你的幫助,希望你的建議能幫到我 =)

祝你有美好的一天 !

對不起

問題是在我推送我的模式之前不要清除 ma 表!!! 沒關系約束是好的=)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM