首页 > 解决方案 > 坚持使用 prisma1 模式设计和数据迁移

问题描述

我有两张桌子,服务和位置。我想在服务表中以数组格式插入多个位置的 id,字段名称为“禁用位置”。每个服务都有一些禁用位置。

我设计了如下模式,当我部署模式更改时它不会返回任何错误。

type Location {
  id: ID! @id
  name: String!
  image: String!
  currency : String!
  lpf: Float!  
  email: String!
}

type Service {
  id: ID! @id
  name: String!
  lastSlot: String
  disableLocations: [Location!]
  startPrice: Json
  hasNote: Int! @default(value: 1)
  createdAt: DateTime! @createdAt
  updatedAt: DateTime! @updatedAt
}

现在,下面是我创建的用于将 JSON 数据插入 Prisma DB 的突变。

 axios({
    url: HOST_GRAPHQL,
    method: 'post',
    data: {
        query: `
            mutation createService($name: String!, $lastSlot: String!, $disableLocations: [LocationCreateManyInput!]!) {
                createService(
                data: {
                    name: $name,
                    lastSlot: $lastSlot,
                    disableLocations :$disableLocations
                },
                ){
                    name,
                    lastSlot,
                    disableLocations { 
                        id
                    }
                }
            }`,
        variables: {
            name: ele.name,
            lastSlot: ele.lastSlot,
            disableLocations: ele.disableLocations,
        }
    }
}).then((result) => {
    console.log(result, "result-----");
});

当我尝试插入一些示例数据时,如下所示,它在禁用位置字段中返回错误。样本数据:

[
  {
    "name": "Painting",
    "lastSlot": "16:00",
    "disableLocations": [
        "ckfoo7rwflsbk09996zw7micu",
        "ckfo6i9cxk85d09994ggr2xny",
        "ckfondzstlq960999ktpwo9ux"
    ]
  },
  {
    "name": "Waterproofing",
    "lastSlot": "15:00",
    "disableLocations": [
        "ckfoo7rwflsbk09996zw7micu",
        "ckfondzstlq960999ktpwo9ux"
    ]
  }
]

返回这样的错误

[
      {
        "message": "Variable '$disableLocations' of type '[LocationCreateManyInput!]!' used in position expecting type '[LocationCreateInput!]'. (line 2, column 84):\n                        mutation createService($name: String!, $lastSlot: String!, $disableLocations: [LocationCreateManyInput!]!) {\n                                                                                   ^\n (line 8, column 45):\n                                    create: $disableLocations\n                                            ^",
        "locations": [
          {
            "line": 2,
            "column": 84
          },
          {
            "line": 8,
            "column": 45
          }
        ]
      }
    ]

如错误中所述,我已尝试为 disableLocations 键入“LocationCreateInput”,但它不起作用。

你们能帮我插入数组吗,如何在 service.disableLocations 字段中插入多个位置 ID。

提前致谢 !!

标签: prismaprisma-graphql

解决方案


据我了解,您正在尝试创建一个记录并通过该字段service将其连接到现有记录。locationdisableLocations

我认为您的语法不正确。disableLocations应该是LocationCreateManyInput您尝试连接到多个location记录时的类型。您应该使用connectAPI。

这是您需要执行的查询

axios({
    url: HOST_GRAPHQL,
    method: 'post',
    data: {
        query: `
            mutation createService($name: String!, $lastSlot: String!) {
                createService(
                data: {
                    name: $name,
                    lastSlot: $lastSlot,
                    disableLocations :{
                        connect:[{
                          id: "ckvks95gb007l08355f17ebno"  // id of existing `location` records
                        },
                        {
                          id: "ckvksb0nr008o08353n8foccn"
                        }]
                      }
                },
                ){
                    name,
                    lastSlot
                }
            }`,
        variables: {
            name: ele.name,
            lastSlot: ele.lastSlot,

        }
    }
})

我删除了disableLocation作为变量并内联数据以使语法更加清晰。但它和变量一样有效。

我有两个额外的建议,我很想知道你的想法:

  1. Prisma1 已过时,不再处于开发阶段。除非您有充分的理由使用 prisma1,否则我强烈建议您迁移到最新版本。我很想知道您的用例以及您使用 Prisma 1 的目的!

  2. 您正在使用 axios 与 Prisma 1 API 进行交互。但是,推荐的方法是使用prisma 客户端库


推荐阅读