P
Prisma4w ago
Seba

PrismaClientValidationError

Hello there, I'm currently stuck with this error:
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 ?.
clientVersion: '6.2.1'
Node.js v22.11.0
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 ?.
clientVersion: '6.2.1'
Node.js v22.11.0
./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 } } }
}
});
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 } } }
}
});
Does anyone know how to solve this?
7 Replies
Prisma AI Help
You selected to wait for the human sages. They'll share their wisdom soon. Grab some tea while you wait, or check out #ask-ai if you'd like a quick chat with the bot anyway!
Seba
SebaOP4w ago
This is my schema:
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])
}
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])
}
RaphaelEtim
RaphaelEtim4w ago
Hi @Seba Can you try this query?
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: {
name_surname_phone_number_email: {
name,
surname,
phone_number: phone_number || '',
email: email || ''
}
},
create: {
name,
surname,
phone_number,
email,
userId
}
}
}
}
});
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: {
name_surname_phone_number_email: {
name,
surname,
phone_number: phone_number || '',
email: email || ''
}
},
create: {
name,
surname,
phone_number,
email,
userId
}
}
}
}
});
Notice how I'm using the compound unique constraint @@unique([name, surname, phone_number, email]) in the where clause of connectOrCreate.
Seba
SebaOP4w ago
Thank you so much, it worked just fine, I wasn't aware of this syntax (name_surname_phone_number_email). Just one last thing: is there a way to connect the info with name_surname_phone_number_email or the userId?
RaphaelEtim
RaphaelEtim4w ago
You can learn about that syntax in the documentation on working with compound unique constraints.
is there a way to connect the info with name_surname_phone_number_email or the userId?
Yes, this is possible
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 }
}
}
}
});
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 }
}
}
}
});
If userId is provided, it uses it as the unique identifier in the where clause. If userId is not provided, it uses the compound unique constraint name_surname_phone_number_email.
Seba
SebaOP4w ago
I thought that, maybe, there was a more clever way to do that (i.e. with OR clause). Thanks anyway and thank you again for the resource.
RaphaelEtim
RaphaelEtim4w ago
You're welcome

Did you find this page helpful?