PrismaClientValidationError 无效的 `prisma.ticket.create()` 调用

问题描述 投票:0回答:1

我目前遇到了这个错误:

PrismaClientValidationError: 
Invalid `   prisma.ticket.create()` invocation in
./src/lib/server/ticket.ts:12:16

   9    phone_number: string,
  10    userId?: number
  11 ) =>
→ 12    prisma.ticket.create({
       data: {
         title: "title",
         description: "description",
         createdBy: {
           connect: undefined
         },
         info: {
           connectOrCreate: {
             where: {
               userId: undefined,
               name: "name",
               surname: "surname",
               email: "email",
               phone_number: "tel",
     ?         id?: Int,
     ?         name_surname_phone_number_email?: InformationNameSurnamePhone_numberEmailCompoundUniqueInput,
     ?         AND?: InformationWhereInput | InformationWhereInput[],
     ?         OR?: InformationWhereInput[],
     ?         NOT?: InformationWhereInput | InformationWhereInput[],
     ?         users?: UserNullableScalarRelationFilter | UserWhereInput | Null,
     ?         tickets?: TicketListRelationFilter
             },
             create: {
               name: "name",
               surname: "surname",
               email: "email",
               phone_number: "tel"
             }
           }
         }
       }
     })

Argument `where` of type InformationWhereUniqueInput needs at least one of `id`, `userId` or `name_surname_phone_number_email` arguments. Available options are marked with ?.
    at wn (./node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma/client/runtime/library.js:29:1363)
    at Vn.handleRequestError (./node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma/client/runtime/library.js:121:6982)
    at Vn.handleAndLogRequestError (./node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma/client/runtime/library.js:121:6663)
    at Vn.request (./node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma/client/runtime/library.js:121:6370)
    at async l (./1522/node_modules/.pnpm/@[email protected][email protected]/node_modules/@prisma/client/runtime/library.js:130:9617)
    at async <anonymous> (./test.ts:3:13) {
  clientVersion: '6.2.1'
}

Node.js v22.11.0

这是我正在运行的文件(./test.ts):

import { createTicket } from './src/lib/server/ticket';

console.log(await createTicket('title', 'description', 'name', 'surname', 'email', 'tel'));

这是我的lib创建新票证的函数(./src/lib/server/ticket.ts):

export const createTicket = (
    title: string,
    description: string,
    name: string,
    surname: string,
    email: string,
    phone_number: string,
    userId?: number
) =>
    prisma.ticket.create({
        data: {
            title,
            description,
            createdBy: { connect: userId ? { id: userId } : undefined },
            info: {
                connectOrCreate: {
                    where: { userId, name, surname, email, phone_number },
                    create: { name, surname, email, phone_number }
                }
            }
        }
    });

如果我将 userId 传递给函数,程序不会失败并返回新票证

这是我正在使用的 prisma 架构 (./prisma/schema.prisma):

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["driverAdapters"]
}

datasource db {
  provider  = "postgresql"
  url       = env("DATABASE_URL")
  directUrl = env("DIRECT_URL")
}

model User {
  id              Int          @id @default(autoincrement())
  username        String?      @unique
  password        String
  role            UserRole     @default(USER)
  tickets         Ticket[]     @relation("CreatedBy")
  assignedTickets Ticket[]     @relation("AssignedTo")
  info            Information?
  createdAt       DateTime     @default(now())
  updatedAt       DateTime     @updatedAt
}

model Ticket {
  id          String       @id @default(nanoid())
  title       String
  description String
  status      TicketStatus @default(PENDING)
  createdBy   User?        @relation("CreatedBy", fields: [userId], references: [id])
  userId      Int?
  assignedTo  User?        @relation("AssignedTo", fields: [operatorId], references: [id])
  operatorId  Int?
  infoId      Int
  info        Information  @relation(fields: [infoId], references: [id])
  createdAt   DateTime     @default(now())
  updatedAt   DateTime     @updatedAt
  closedAt    DateTime?
}

model Information {
  id           Int      @id @default(autoincrement())
  name         String
  surname      String
  phone_number String?
  email        String?
  userId       Int?     @unique
  users        User?    @relation(fields: [userId], references: [id])
  tickets      Ticket[]

  @@unique([name, surname, phone_number, email])
}

enum UserRole {
  USER
  OPERATOR
  ADMIN
}

enum TicketStatus {
  PENDING
  ONGOING
  COMPLETED
}

有谁知道如何解决这个问题吗?

node.js typescript orm prisma
1个回答
0
投票

发生该错误的原因是 prisma 尝试使用

connectOrCreate
以及对
Information
模型的唯一约束,其中包括
userId
name
surname
phone_number
email
。由于
userId
也是唯一的,prisma 希望定义这些字段来连接或创建信息记录。如果任何字段为
undefined
,prisma 将失败,因为
undefined
不是数据库字段的有效值。但是,
userId
可以是
null

© www.soinside.com 2019 - 2024. All rights reserved.