本文介绍了Prisma upsert提供的许多问题列表<;Json&>,预期为SimilarCreateWithoutMovieInput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用"@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] }
代码
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,
})
这里是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
应为其他Movies
嵌套在特定Movie
对象中的数组[]
,本身不能
我没有在生成的类型上使用Prisma upsert的经验,我收到了上面的错误。我希望插入一个Movie
,同时插入与Movie
相关的多个Similar
记录。
我已尝试使用connectOrCreate
,但它不支持在按预期创建movie
的一条记录时创建多条similar
记录
我如何实现这一点?
资源
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
推荐答案
问题是PRISMAcreateMany
在SQLite上不可用,而在PostgreSQL连接器上可用。
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,
})
这篇关于Prisma upsert提供的许多问题列表<;Json&>,预期为SimilarCreateWithoutMovieInput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!