prisma .upsert 的问题,未知参数

Problem with prisma .upsert, Unkown argument

我对 prisma upsert() 有疑问,我得到信息:

PrismaClientValidationError: Invalid prisma.prismaUser.upsert() invocation:

{ where: { email: 'viola@prisma.io' ~~~~~ }, update: { name: 'Viola the Magnificent' }, create: { email: 'viola@prisma.io', name: 'Viola the Magnificent', profileViews: 0, role: 'admin' } }

Unknown arg email in where.email for type prismaUserWhereUniqueInput. Did you mean id? Available args: type prismaUserWhereUniqueInput { id?

我的代码: schema.prisma

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model prismaUser {
  id           Int    @id @default(autoincrement())
  name         String @db.VarChar(255)
  email        String @db.VarChar(255)
  profileViews Int
  role         String @db.VarChar(255)
}

node.js

const { PrismaClient } = require("@prisma/client");

const prisma = new PrismaClient();

// A `main` function so that you can use async/await
async function main() {
  await prisma.prismaUser.upsert({
    where: {
      email: "viola@prisma.io",
    },
    update: {
      name: "Viola the Magnificent",
    },
    create: {
      email: "viola@prisma.io",
      name: "Viola the Magnificent",
      profileViews: 0,
      role: "admin",
    },
  });
}

main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });

谁能帮我解释一下哪里出了问题?

upsert 查询的 where 子句中的字段应该是唯一的。

在这种情况下,电子邮件字段不是唯一的,因此您会收到此错误。

通过向电子邮件字段添加 @unique 属性来更新架构文件将解决问题。

model prismaUser {
  id           Int    @id @default(autoincrement())
  name         String @db.VarChar(255)
  email        String @unique @db.VarChar(255)
  profileViews Int
  role         String @db.VarChar(255)
}