0

I am using planetscale and I have a survey model that connects to the user who submitted the survey.

My schema is as follows

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

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
  relationMode = "prisma"
}
model Survey {
  id        String   @id @default(cuid())
  title     String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  authorId  String
  author    User     @relation(fields: [authorId], references: [id])
  answers   Answer[]

  @@index([authorId])
}
model User {
  id            String    @id @default(cuid())
  name          String?
  email         String?   @unique
  emailVerified DateTime?
  image         String?
  password      String?   @default(cuid())
  role          String    @default("USER")
  surveys       Survey[]  
}

This is where I am calling the create survey

const author = await db.user.findUnique({
      where: { email: user.email as string },
    });

    if (!author) {
      return res.status(401).json({ error: "Not authenticated" });
    }

    console.log("author", author);

    const survey = await db.survey.create({
      data: {
        answers: {
          createMany: {
            data: parsedAnswers as any,
          },
        },

        title: req.body.title,
        author: {
          connect: {
            id: author.id,
          }
        },
      },
      include: {
        author: true,
      },
    });

When I only use connect it gives me this error

Error: 
Invalid `prisma.survey.create()` invocation:
...
    author: {
      connect: {
        id: "****"
      }
    },
+   authorId: String
  },
  include: {
    author: true
  }
}

Argument `authorId` is missing.
    at handleSubmit (webpack-internal:///(app-client)/./app/assessment2/Form.tsx:74:23)

and when I try adding the id

    title: 'B',
    author: {
      connect: {
        id: '***'
      }
    },
    authorId: '***'
    ~~~~~~~~
  },
  include: {
    author: true
  }
}

Unknown arg `authorId` in data.authorId for type SurveyCreateInput. Did you mean `author`? Available args:
type SurveyCreateInput {
  id?: String
  title: String
  createdAt?: DateTime
  updatedAt?: DateTime
  author: UserCreateNestedOneWithoutSurveysInput
  answers?: AnswerCreateNestedManyWithoutSurveyInput
  reviewedBy?: UserCreateNestedOneWithoutReviewsInput
}

I am using next js app dir and I want to connect the survey to a user

juliomalves
  • 42,130
  • 20
  • 150
  • 146
ibo
  • 51
  • 3

0 Answers0