Prisma更新查询仅允许通过ID字段进行更新,而不允许其他任何字段进行更新。

4
  const response = await prisma.teamMember.update({
    where: {
      teamId,
      userId: memberToEditId,
    },
    data: {
      role,
    },
  });

Argument where of type TeamMemberWhereUniqueInput needs exactly one argument, but you provided teamId and userId. Please choose one. Available args:
type TeamMemberWhereUniqueInput {
  id?: String
}
Unknown arg `teamId` in where.teamId for type TeamMemberWhereUniqueInput. Did you mean `id`? Available args:
type TeamMemberWhereUniqueInput {
  id?: String
}
Unknown arg `userId` in where.userId for type TeamMemberWhereUniqueInput. Did you mean `id`? Available args:
type TeamMemberWhereUniqueInput {
  id?: String
}

嗨,大家好。我正在尝试根据表格中特定的值来更新一个特定的文档,但它似乎只允许我使用表格的主键?我的模式如下:

model TeamMember {
  id        String   @id @default(cuid())
  teamId    String
  userId    String
  role      Role     @default(MEMBER)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model Team {
  id         String       @id @default(cuid())
  name       String
  createdAt  DateTime     @default(now())
  updatedAt  DateTime     @updatedAt
  TeamMember TeamMember[]
}


model User {
  id            String       @id @default(cuid())
  name          String?
  email         String?      @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  TeamMember    TeamMember[]
  theme         Theme        @default(LIGHT)
}

为了解决这个问题,我可以临时创建一个单独的findFirst查询,并使用返回的行来获取我想要更新的行的ID。尽管这样做是可以的,但我知道不需要这样做,而且当一个查询就能正常工作时,使用两个查询有点丑陋。
非常感谢您的帮助。
2个回答

7

Prisma需要唯一识别需要更新的单个记录,因此强制执行whereUnique约束条件。

目前,您可以使用updateMany作为解决方法,这个唯一约束条件不会被强制执行在updateMany中。


0
从Prisma v5开始(关于extendedWhereUnique的一般介绍),您可以直接更新@unique字段:
model TeamMember {
  id        String   @id @default(cuid())
  teamId    String   
  userId    String   @unqiue // <-- Important
  role      Role     @default(MEMBER)
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
  user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}

  const response = await prisma.teamMember.update({
    where: {
      teamId,
      userId: memberToEditId, // <-- this operation will now work because it is unique
    },
    data: {
      role,
    },
  });

关于extendedWhereUnique的参考: https://www.prisma.io/docs/concepts/components/preview-features/client-preview-features#preview-features-promoted-to-general-availability

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接