最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Problem with prisma .upsert, Unkown argument - Stack Overflow

programmeradmin8浏览0评论

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

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

{ where: { email: '[email protected]' ~~~~~ }, update: { name: 'Viola the Magnificent' }, create: { email: '[email protected]', 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: 

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: "[email protected]",
    },
    update: {
      name: "Viola the Magnificent",
    },
    create: {
      email: "[email protected]",
      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?

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

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

{ where: { email: '[email protected]' ~~~~~ }, update: { name: 'Viola the Magnificent' }, create: { email: '[email protected]', 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: "[email protected]",
    },
    update: {
      name: "Viola the Magnificent",
    },
    create: {
      email: "[email protected]",
      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?

Share Improve this question asked Apr 19, 2022 at 14:15 robokonkrobokonk 1411 silver badge10 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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)
}
发布评论

评论列表(0)

  1. 暂无评论