I'm currently stuck with this error:
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/@prisma+client@6.2.1_prisma@6.2.1/node_modules/@prisma/client/runtime/library.js:29:1363)
at Vn.handleRequestError (./node_modules/.pnpm/@prisma+client@6.2.1_prisma@6.2.1/node_modules/@prisma/client/runtime/library.js:121:6982)
at Vn.handleAndLogRequestError (./node_modules/.pnpm/@prisma+client@6.2.1_prisma@6.2.1/node_modules/@prisma/client/runtime/library.js:121:6663)
at Vn.request (./node_modules/.pnpm/@prisma+client@6.2.1_prisma@6.2.1/node_modules/@prisma/client/runtime/library.js:121:6370)
at async l (./1522/node_modules/.pnpm/@prisma+client@6.2.1_prisma@6.2.1/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
This is the file I'm running (./test.ts):
import { createTicket } from './src/lib/server/ticket';
console.log(await createTicket('title', 'description', 'name', 'surname', 'email', 'tel'));
This is my lib's function to create a new ticket (./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 }
}
}
}
});
If I pass the userId to the function the program doesn't fail and the new ticket is returned
This is the prisma schema I'm using (./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
}
Does anyone know how to solve this?
This is the solution I've come up to:
export const createTicket = (
title: string,
description: string,
name: string,
surname: string,
email: string | null,
phone_number: string | null,
userId?: number
) =>
prisma.ticket.create({
data: {
title,
description,
createdBy: userId ? { connect: { id: userId } } : undefined,
info: {
connectOrCreate: {
where: userId
? { userId }
: { name_surname_phone_number_email: { name, surname, phone_number, email } },
create: { name, surname, email, phone_number, userId }
}
}
}
});
I wasn't aware of the compound unique constraint syntax.
With this syntax I can search using the @@unique
constraint on Information
.