【问题标题】:How to update composite type model in Prisma?如何在 Prisma 中更新复合类型模型?
【发布时间】:2022-10-14 04:35:53
【问题描述】:

我正在尝试在 Prisma 的复合类型模型中实现更新。

这是我的数据结构:

{
  "name":"toy",
  "data":{
     "sports":{
        "currentState":"false"
    },
     "business":{
        "currentState":"false"
    }
  }
   
}

这是我的更新代码:

const updatedSource = await prisma.sources.update({
            where: {
                name: 'toy'
            },
            data: {
                data: {
                    sports: {
                        currentState: "true"
                    }
                }
            },
        })

这是我的架构文件

type SourcesData {
  business      SourcesDataState
  sports        SourcesDataState
}

type SourcesDataState {
  currentState StateData[]
}

type StateData {
  title String
  url   String
}

model sources {
  id           String   @id @default(auto()) @map("_id") @db.ObjectId
  data         SourcesData
  name         String   @unique
}


当我执行上述逻辑时,我得到错误:Unknown arg `sports` in data.data.sports for type SourcesDataUpdateEnvelopeInput. Did you mean `set`? Available args:

请指导我在更新时缺少的内容。

【问题讨论】:

  • 您可以在问题中添加您的架构文件吗?
  • @Nurul Sundarani,我添加了架构文件

标签: prisma


【解决方案1】:

TypeScript 应该非常有助于告诉您在与 Prisma 交互时可以使用或不可以使用哪些参数。我强烈建议使用包含 TypeScript typehinting/Intellisense 的代码编辑器,以便在使用 Prisma 进行开发时查看有关 TypeScript 使用的错误和警告。

在您的错误中显示Available args 的地方应该告诉您prisma.sports.update 实际期望的参数。如果我不得不猜测(这可能不准确,但您必须查看 TypeScript 才能确切知道它应该是什么),它应该看起来像这样:

const updatedSource = await prisma.sources.update({
    where: {
        name: 'toy'
    },
    data: {
        data: {
            update: {
                sports: {
                    update: {
                        currentState: {
                            set: ["true"]
                        }
                    }
                }
            }
        }
    },
})

我强烈建议阅读 Prisma 关于更新相关/嵌套记录的文档:https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#update-a-specific-related-record

【讨论】:

    猜你喜欢
    • 2022-08-22
    • 2021-11-14
    • 2021-11-30
    • 1970-01-01
    • 2023-01-31
    • 1970-01-01
    • 2020-08-18
    • 1970-01-01
    • 2016-08-01
    相关资源
    最近更新 更多