English 中文(简体)
PrismaClientValidation Error: Invalid `prisma.user.create()' in Occupation:
原标题:PrismaClientValidationError: Invalid `prisma.user.create()` invocation:

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?

问题回答

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

<代码>npx prismamigration reset

我已同指挥官一道解决这一问题。

npx prisma generate

您需要调整和澄清你的法典

     //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])
  
}




相关问题
Allow RESTful DELETE method in asp.net mvc?

im currently setting up asp.net to accept DELETE http verb in the application. However, when i send "DELETE /posts/delete/1" i always get a 405 Method not allow error. I tried to take a look at ...

Most appropriate API for URL shortening service

I ve just finished an online service for shortening URLs (in php5 with Zend Framework); you can enter an URL and you get an short URL (like tinyurl and such sites). I m thinking about the API for ...

Use HTTPClient or HttpUrlConnection? [closed]

We re implementing a REST client on JRE 1.4. Seems two good options for a client REST framework are HttpClient and HttpUrlConnection. Is there a reason to use HttpClient over the JRE s ...

Why can t I find the truststore for an SSL handshake?

I m using the Spring RESTTemplate on the client side to make calls to a REST endpoint. The client in this case is a Spring app and Tomcat is the servlet container. I m running into issues making a ...

Which Http redirects status code to use?

friendfeed.com uses 302. bit.ly uses 301. I had decided to use 303. Do they behave differently in terms of support by browsers ?

Three Step Buyonline The RESTful way

We are re-developing our buyonline functionality and we are doing it the RESTful way. The process is a three step one and the customer is asked to enter data at each step. Let s say the three URL s ...

热门标签