Prisma 嵌套创建引发未知 arg 错误,而 prisma 示例工作

Posted

技术标签:

【中文标题】Prisma 嵌套创建引发未知 arg 错误,而 prisma 示例工作【英文标题】:Prisma nested create throws unknown arg error while prisma example works 【发布时间】:2021-12-03 15:35:51 【问题描述】:

我在使用 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

Prisma example:

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
      ,
    ,
  ,
)

是否可以编写深度嵌套的创建操作? 我需要为此使用交易吗? 有其他选择吗?

谢谢!

【问题讨论】:

【参考方案1】:

您需要像这样通过connect 传递它,而不是直接传递localeFlagId

localeFlag:  connect:  id: 1  

【讨论】:

你是对的!使用 connect 而不是直接 id 解决了嵌套创建的问题。该错误非常具有误导性。非常感谢! 是的,我们正在为此创建更好的错误消息:)

以上是关于Prisma 嵌套创建引发未知 arg 错误,而 prisma 示例工作的主要内容,如果未能解决你的问题,请参考以下文章

Prisma 2:未知 arg `where` 在 select.count.where 类型未定义

在 Relay 中创建嵌套突变会引发未知字段

prisma2:如何获取嵌套字段?

自定义功能用户的角色不起作用,嵌套查询,prisma graphql

使用 Prisma 的嵌套创建查询返回未定义

使用 prisma,如何从嵌套写入中访问新创建的记录(先更新,然后在其中创建)