prisma - 坚持使用 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。
提前致谢 !!
解决方案
据我了解,您正在尝试创建一个记录并通过该字段service
将其连接到现有记录。location
disableLocations
我认为您的语法不正确。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
作为变量并内联数据以使语法更加清晰。但它和变量一样有效。
我有两个额外的建议,我很想知道你的想法:
Prisma1 已过时,不再处于开发阶段。除非您有充分的理由使用 prisma1,否则我强烈建议您迁移到最新版本。我很想知道您的用例以及您使用 Prisma 1 的目的!
您正在使用 axios 与 Prisma 1 API 进行交互。但是,推荐的方法是使用prisma 客户端库。
推荐阅读
- html - 右对齐导航栏
- rpc - Corda 网络服务器产生异常“用户无权使用目标执行 RPC 调用 nodeInfo”
- vue.js - 数据未显示在 console.log 中,但在模板中呈现良好
- java - 使用多个数据源时,如何使用与 Repository 相同的名称?
- android - 在 Android 21 及更高版本中禁用锁定屏幕
- python - Pandas 数据框:在日期时间索引中按月获取列中的元素?
- python - IndexError:索引只能有一个省略号
- android - 无法覆盖 AppTheme 字体
- javascript - If Else 语句响应中的 JavaScript For 循环
- vue.js - Parent的数据函数被调用两次