Issue
I tried to create a new project with Prisma Client, but I'm getting an error when passing it with args
, even just an empty array list like this []
.
this is my 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 this is my 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;
}
this 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
}
})
}
and this 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
I'm curious about how to generate TypeScript types based on the Prisma model. If you guys have any best practices, I'd appreciate it. Thanks in advance!
Solution
You can import the types from Prisma directly after you run npx prisma generate
import { Image, Project } from "@prisma/client"
If you open the file where they are defined in, you can get more detailed types for your models.
node_modules/.prisma/client/index.d.ts
There are a lot of types inside this file, you just have to find the ones you need for specific uses.
Answered By - SlothOverlord
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.