While attempting to initiate a new project using Prisma Client, I encountered an error when passing it with args
, even when using an empty array list such as []
.
Here is the Prisma model:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model Project {
id String @id @default(cuid())
name String
date DateTime @default(now())
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
type String
client String?
images Image[]
}
model Image {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @default(now())
title String?
alt String?
description String?
src String
project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
projectId String
}
And here is the TypeScript type:
export interface Project {
name: string;
date: Date;
type: string;
client: string;
images?: Image[];
}
export interface Image {
title: string;
alt: string;
description: string;
src: string;
projectId?: string;
}
Below is my mutation:
Attempt 1
createProject: async (args: Project) => {
const project = await prisma.project.create({
data: {
client: args.client,
date: args.date,
name: args.name,
type: args.type,
images: args.images
}
})
}
Attempt 2
createProject: async (args: Project) => {
const project = await prisma.project.create({
data: {
client: args.client,
date: args.date,
name: args.name,
type: args.type,
images: []
}
})
}
Attempt 3
createProject: async (args: Project & { images?: Image[] }) => {
const project = await prisma.project.create({
data: {
client: args.client,
date: args.date,
name: args.name,
type: args.type,
images: args.images
}
})
}
Additionally, here is the error from the Prisma client:
Type 'Image[] | undefined' is not assignable to type 'ImageUncheckedCreateNestedManyWithoutProjectInput | ImageCreateNestedManyWithoutProjectInput | undefined'. Type 'Image[]' is not assignable to type 'ImageUncheckedCreateNestedManyWithoutProjectInput | ImageCreateNestedManyWithoutProjectInput | undefined'.ts(2322)
index.d.ts(3077, 5): The expected type comes from property 'images' which is declared here on type '(Without<ProjectCreateInput, ProjectUncheckedCreateInput> & ProjectUncheckedCreateInput) | (Without<...> & ProjectCreateInput)' (property) images: Image[] | undefined
If you have any best practices on generating TypeScript types based on the Prisma model, I would greatly appreciate the guidance. Thank you in advance!