Reputation: 141
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
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?
Upvotes: 5
Views: 5553
Reputation: 7588
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)
}
Upvotes: 7