Skip to content
Advertisement

Problem with prisma .upsert, Unkown argument

I have problem with prisma upsert(), I get info:

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?

My code: 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();
  });

Anyone can help me and explain what is wrong?

Advertisement

Answer

The field in where clause of the upsert query should be unique.

In this case, the email field is not unique due to which you are getting this error.

Updating schema file by adding @unique attribute to the email field will solve the issue.

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)
}
Advertisement