Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Typescript: Prisma relation list field getting error when passed with argument

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading