Search code examples
node.jsormrelational-databaseprismaprisma2

Prisma upsertmany issue Provided List<Json>, expected SimilarCreateWithoutMovieInput


using "@prisma/client": "^2.25.0"

 on prisma.upsertOneMovie. Provided List<Json>, expected SimilarCreateWithoutMovieInput or SimilarCreateWithoutMovieInput or SimilarUncheckedCreateWithoutMovieInput or SimilarUncheckedCreateWithoutMovieInput:
[0] type SimilarCreateWithoutMovieInput {
[0]   id: Int
[0]   backdrop_path: String
[0]   title: String
[0]   name: String
[0]   release_date: DateTime
[0]   overview: String
[0]   show: TVShowCreateNestedOneWithoutSimilarInput
[0] }

code

const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
...
        let result = await apiRequest
        let similar = result.similar.results.map(similar_movie => {
          return {
            id: similar_movie.id, 
            backdrop_path: similar_movie.backdrop_path, 
            title: similar_movie.title,
            name: similar_movie.name,
            release_date: similar_movie.release_date,
            overview: similar_movie.overview,
            movieId: result.id
          }
        })

        const movie = {
        id: result.id,
...
        similar: {
            upsert: similar
          }
        }
        const upsertMovie = await prisma.movie.upsert({
          where: { id: movie.id },
          update: movie,
          create: movie,
        })

here is the schema.prisma

model Movie {
  id                    Int         @id
...other fields and attributes...
similar             Similar[]
}

model Similar {
    id              Int         @id @default(autoincrement())
    tmdb_id         Int         @unique
    backdrop_path   String
    title           String?
    name            String?
    release_date    String
    overview        String
    movie           Movie?      @relation(fields: [movie_id], references: [id])
    show            TVShow?         @relation(fields: [show_id], references: [id])
    movie_id    Int?
    show_id Int?
}

Similar is supposed to be an array [] of other Movies nested in a specific Movie object, cant be itself

I do not have experience with prisma upsert on generated types and I am getting the above error. Im expecting to upsert a Movie and at the same time upsert multiple records of Similar that are related to Movie.

i have tried using connectOrCreate but it does not support creating multiple records of similar while creating one record of movie as expected
how do I achieve that?

resources
https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#nested-writes
https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#connect-or-create-a-record


Solution

  • Issue was that prisma createMany is not available on sqlite, and on the postgresql connector, it is.

    const movie = {
            ...result,
            similar: {
              connectOrCreate: result.similar.map(s => ({
                create: s,
                where: { id: s.id },
              }))
            }
          }
    
    const upsertMovie = await prisma.movie.upsert({
              where: { id: movie.id },
              update: movie,
              create: movie,
            })