Prisma 嵌套创建在 Prisma 示例运行时抛出未知 arg 错误
Prisma nested create throws unknown arg error while prisma example works
我在使用 prisma 嵌套创建时遇到了一些问题。第二个嵌套创建不起作用,因为它抱怨该 arg 不存在。如果我手动编写嵌套创建,我可以用打字稿看到“音频”字段是可选的并且存在于该模型上。
这是模型
model CulturalHeritage {
id Int @id @default(autoincrement())
duration Int
categoryId Int
category CulturalHeritageCategory @relation(fields: [categoryId], references: [id])
published Boolean @default(true)
mediaId Int?
media Media? @relation(fields: [mediaId], references: [id])
created DateTime @default(now())
updated DateTime? @updatedAt
expired DateTime?
CulturalHeritageLocale CulturalHeritageLocale[]
MediaCulturalHeritage MediaCulturalHeritage[]
CulturalHeritageExibit CulturalHeritageExibit[]
}
model CulturalHeritageLocale {
id Int @id @default(autoincrement())
culturalHeritageId Int
culturalHeritage CulturalHeritage @relation(fields: [culturalHeritageId], references: [id])
description String @default("<p></p>")
title String
audio Media? @relation(fields: [audioId], references: [id])
audioText String?
audioId Int?
localeFlag LocaleFlag @relation(fields: [localeFlagId], references: [id])
localeFlagId Int
created DateTime @default(now())
updated DateTime? @updatedAt
expired DateTime?
}
model Media {
id Int @id @default(autoincrement())
path String
extension String
name String
type String
size Int
created DateTime @default(now())
updated DateTime? @updatedAt
expired DateTime?
CulturalHeritage CulturalHeritage[]
CulturalHeritageLocale CulturalHeritageLocale[]
ExibitLocale ExibitLocale[]
MediaExibit MediaExibit[]
MediaCulturalHeritage MediaCulturalHeritage[]
}
函数:
//...
return await prisma.culturalHeritage.create({
include: {
media: true,
CulturalHeritageLocale: {
include: {
audio: true,
},
},
},
data: {
duration: args.data.duration,
category: { connect: { id: args.data.categoryId } },
published: args.data.published,
CulturalHeritageLocale: {
// create: args.localeData,
create: [
{
description: '<p>123</p>',
title: '123',
audioText: '',
localeFlagId: 2,
},
{
description: '<p>1</p>',
title: '1',
audio: { // <--- PRISMA ERROR HERE, TYPESCRIPTS STATES THAT ARG IS OPTIONAL AND EXISTS
create: {
name: 'file_example_OOG_1MG.ogg',
path: '163e6e51-7a6b-4d36-acf6-1328cdd87caf.ogg',
extension: 'ogg',
type: 'audio/ogg',
size: 1089524,
},
},
audioText: 'lll',
localeFlagId: 1,
},
],
},
media: { create: args.data.media.create[0] },
},
});
错误:
Unknown arg `audio` in data.CulturalHeritageLocale.create.1.audio for type CulturalHeritageLocaleUncheckedCreateWithoutCulturalHeritageInput. Did you mean `audioId`? Available args:
type CulturalHeritageLocaleUncheckedCreateWithoutCulturalHeritageInput {
id?: Int
description?: String
title: String
audioText?: String | Null
audioId?: Int | Null
localeFlagId: Int
created?: DateTime
updated?: DateTime | Null
expired?: DateTime | Null
}
const user = await prisma.user.create({
data: {
email: 'yvette@prisma.io',
name: 'Yvette',
posts: {
create: [
{
title: 'How to make an omelette',
categories: {
create: { // <--- Works fine
name: 'Easy cooking',
},
},
},
{ title: 'How to eat an omelette' },
],
},
},
include: {
// Include posts
posts: {
include: {
categories: true, // Include post categories
},
},
},
})
是否可以编写深度嵌套的创建操作?
我需要为此使用交易吗?
还有其他选择吗?
谢谢!
不是直接传递 localeFlagId
,而是需要像这样通过 connect
传递它:
localeFlag: { connect: { id: 1 } }
我在使用 prisma 嵌套创建时遇到了一些问题。第二个嵌套创建不起作用,因为它抱怨该 arg 不存在。如果我手动编写嵌套创建,我可以用打字稿看到“音频”字段是可选的并且存在于该模型上。
这是模型
model CulturalHeritage {
id Int @id @default(autoincrement())
duration Int
categoryId Int
category CulturalHeritageCategory @relation(fields: [categoryId], references: [id])
published Boolean @default(true)
mediaId Int?
media Media? @relation(fields: [mediaId], references: [id])
created DateTime @default(now())
updated DateTime? @updatedAt
expired DateTime?
CulturalHeritageLocale CulturalHeritageLocale[]
MediaCulturalHeritage MediaCulturalHeritage[]
CulturalHeritageExibit CulturalHeritageExibit[]
}
model CulturalHeritageLocale {
id Int @id @default(autoincrement())
culturalHeritageId Int
culturalHeritage CulturalHeritage @relation(fields: [culturalHeritageId], references: [id])
description String @default("<p></p>")
title String
audio Media? @relation(fields: [audioId], references: [id])
audioText String?
audioId Int?
localeFlag LocaleFlag @relation(fields: [localeFlagId], references: [id])
localeFlagId Int
created DateTime @default(now())
updated DateTime? @updatedAt
expired DateTime?
}
model Media {
id Int @id @default(autoincrement())
path String
extension String
name String
type String
size Int
created DateTime @default(now())
updated DateTime? @updatedAt
expired DateTime?
CulturalHeritage CulturalHeritage[]
CulturalHeritageLocale CulturalHeritageLocale[]
ExibitLocale ExibitLocale[]
MediaExibit MediaExibit[]
MediaCulturalHeritage MediaCulturalHeritage[]
}
函数:
//...
return await prisma.culturalHeritage.create({
include: {
media: true,
CulturalHeritageLocale: {
include: {
audio: true,
},
},
},
data: {
duration: args.data.duration,
category: { connect: { id: args.data.categoryId } },
published: args.data.published,
CulturalHeritageLocale: {
// create: args.localeData,
create: [
{
description: '<p>123</p>',
title: '123',
audioText: '',
localeFlagId: 2,
},
{
description: '<p>1</p>',
title: '1',
audio: { // <--- PRISMA ERROR HERE, TYPESCRIPTS STATES THAT ARG IS OPTIONAL AND EXISTS
create: {
name: 'file_example_OOG_1MG.ogg',
path: '163e6e51-7a6b-4d36-acf6-1328cdd87caf.ogg',
extension: 'ogg',
type: 'audio/ogg',
size: 1089524,
},
},
audioText: 'lll',
localeFlagId: 1,
},
],
},
media: { create: args.data.media.create[0] },
},
});
错误:
Unknown arg `audio` in data.CulturalHeritageLocale.create.1.audio for type CulturalHeritageLocaleUncheckedCreateWithoutCulturalHeritageInput. Did you mean `audioId`? Available args:
type CulturalHeritageLocaleUncheckedCreateWithoutCulturalHeritageInput {
id?: Int
description?: String
title: String
audioText?: String | Null
audioId?: Int | Null
localeFlagId: Int
created?: DateTime
updated?: DateTime | Null
expired?: DateTime | Null
}
const user = await prisma.user.create({
data: {
email: 'yvette@prisma.io',
name: 'Yvette',
posts: {
create: [
{
title: 'How to make an omelette',
categories: {
create: { // <--- Works fine
name: 'Easy cooking',
},
},
},
{ title: 'How to eat an omelette' },
],
},
},
include: {
// Include posts
posts: {
include: {
categories: true, // Include post categories
},
},
},
})
是否可以编写深度嵌套的创建操作? 我需要为此使用交易吗? 还有其他选择吗?
谢谢!
不是直接传递 localeFlagId
,而是需要像这样通过 connect
传递它:
localeFlag: { connect: { id: 1 } }