PrismaClientValidationError: Invalid `prisma.user.create()` invocation:
Asked Answered
E

5

7

I am trying to create an API using Next.js & Prisma. I have two model user & profile. I want to create user & also profile field from req.body using postman.

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

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

model User {
  id Int @id @default(autoincrement())
  name String
  email   String @unique
  password String
  profile Profile?
}

model Profile {
  id     Int     @default(autoincrement()) @id
  bio    String?
  user   User    @relation(fields: [userId], references: [id])
  userId Int   @unique
}

here my create function:

 //Create User
   let { name, email, password,  } = req.body;
    const createUser = await prisma.user.create({
      data: {
        name: name,
        email: email,
        password: user_password,
        profile: {
          create: { bio: 'Bsc' }
          },  
      }
    });
    res.status(201).json({ error: false, msg: "User Create Successfuly" });

After URL hit I got an error. PrismaClientValidationError: Invalid prisma.user.create() invocation Unknown arg profile in data.profile for type UserCreateInput. Did you mean email? Available args:type UserCreateInput

How can I solve this?

Esther answered 4/7, 2021 at 16:24 Comment(0)
T
8

I have resolved this issue with the command

npx prisma generate
Tropism answered 18/10, 2023 at 12:45 Comment(1)
man this helped me a lot I was just struggling fooling around very much thanks!!Achievement
H
1

I had the same issue, stop the server from running first and try to : npx yarn-upgrade-all

yarn add @prisma/client

npx prisma migrate reset

Hominy answered 6/7, 2023 at 17:41 Comment(1)
This will drop your entire database and tables... Thanks.Nestorius
C
0

you need to adjust and clarify your code

     //Create User
//because you had already created  and get variables 
 from the body here
   let { name, email, password,  } = req.body;
    const createUser = await prisma.user.create({
      data: {
//no need to reassign them here
        name,
        email,
        password,
//the  profile creation is missing elements here, you just put  the 'bio' where are //the other elements???
        profile: {
          create: { bio: 'Bsc' }
          },  
      }
    });

Note: Lines with + are required, lines with ? are optional. In your schema, what kind of relation is this? The User belongs to Profile or the Profile belongs to the user?

    model User {
  id Int @id @default(autoincrement())
  name String
  email   String @unique
  password String
  profile Profile  ?//<= Adjust  the Foreign key. Do you mean an array of 
             //profile or  a unique profile?
}

model Profile {
  id     Int     @default(autoincrement()) @id
  bio    String?
  userId Int   @unique// userId goes before  the relation declaration
  user   User    @relation(fields: [userId], references: [id])
  
}
Coupon answered 2/12, 2023 at 13:41 Comment(0)
A
0

use type keyword when importin NextAuthOptions This solution worked for me.

import { type NextAuthOptions } from "next-auth";
Ahders answered 19/4 at 17:23 Comment(0)
P
0

This error also comes when trying to pass a Parameter with a different datatype.

For example: expect was INT and you are sending STRING something like this.

we need to match DataType as well while sending the data to the Route.

Prompt answered 3/7 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.