【问题标题】:Stuck with prisma1 schema design and data migration坚持使用 prisma1 模式设计和数据迁移
【发布时间】:2021-12-16 08:15:16
【问题描述】:

我有两张桌子,服务和位置。我想在服务表中以数组格式插入多个位置的 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。

提前致谢!!

【问题讨论】:

    标签: prisma prisma-graphql


    【解决方案1】:

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

    我认为你的语法不正确。 disableLocations 应该是 LocationCreateManyInput 类型,因为您正在尝试连接到多个 location 记录。您应该使用connect API。

    这是你需要做的查询

    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,否则我强烈建议您使用 migrating to the latest version。我很想知道您的用例以及您使用 Prisma 1 的目的!

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-16
      • 2010-11-26
      • 1970-01-01
      • 2012-02-25
      • 2014-08-12
      • 2011-01-04
      • 1970-01-01
      • 2014-08-15
      相关资源
      最近更新 更多