sql - 在 Prisma/Graphql 一对一和一对多上遇到困难
问题描述
我很难弄清楚我创建了这个模式的 1 对 1 和 1 对多的关系,然后我的 graphql 就是从中派生的
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
model Games {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
short_hand String @unique @db.VarChar(255)
name String @unique @db.VarChar(255)
in_boxes Box[]
set Sets[]
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
username String @unique @db.VarChar(255)
password String @db.VarChar(255)
role Role @default(USER)
first_name String? @db.VarChar(255)
last_name String? @db.VarChar(255)
store String? @db.VarChar(255)
boxes Box[]
}
model Store {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String @unique @db.VarChar(255)
}
model Box {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
box_number String @db.VarChar(100)
box_second_number String? @db.VarChar(100)
set String @db.VarChar(255)
set_list Sets @relation(fields: [set], references: [name])
gameid Int? @default(1)
game Games? @relation(fields: [gameid], references: [id])
User User? @relation(fields: [userId], references: [id])
userId Int?
}
model Sets {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String @unique @db.VarChar(255)
code String @unique @db.VarChar(255)
children String[]
in_boxes Box[]
game String? @db.VarChar(255)
gamerel Games? @relation(fields: [game], references: [short_hand])
edition String?
}
enum Role {
USER
ADMIN
CHRIS
}
基本上,用户将拥有他们拥有的盒子(盒子有一个游戏类型并包含一个父集,它有自己的集合代码并包含集合子(数组)它自己的游戏类型只有名称和简码我的主要问题是当我尝试使用解析器代码创建一个集合
the graphql
mutation {
createBox (input:{box_number:"001", secondary_number:"A", game:{name:"yu"}, set:"Crucibile of War Unlimit"}) {
box_number
set
game
id
}
}
解析器
createBox: async (_: any, { input }: any, context: Context) => {
const find = await context.prisma.games.findFirst({
where: {name: {contains:input.game[0].name,
mode:"insensitive"}}
}
);
console.log(find);
console.log(input.set);
return await context.prisma.box.create({
data: {
box_number: input.box_number,
box_second_number: input.secondary_number,
gameid: find?.id,
set: {connect: {
name: input.set
},
},
},
});
},
我明白了
" Foreign key constraint failed on the field: `Box_set_fkey (index)`",
" at cb
我真的很困惑如何让它工作
解决方案
在这种情况下,set
直接期望关系,所以你不需要connect
在这里。这应该有效:
const find = await prisma.games.findFirst({
where: { name: { contains: input.game[0].name } },
})
await prisma.box.create({
data: {
box_number: input.box_number,
box_second_number: input.secondary_number,
gameid: find?.id,
set: input.set,
},
})
推荐阅读
- visual-studio - 打开我的项目 exe 后缺少 mfc120d.dll
- powershell - 抑制 powershell 中的错误并显示自定义错误消息
- wordpress - 外观菜单 - 将新项目添加到菜单顶部
- audio - 如何用ffmpeg覆盖两个音频?
- reactjs - 不能使用 history.push 到异步路由,React JS
- unity3d - 如何从网格中获取对象的旋转/方向?
- angular - 角路由器方向“前进”
- android - Google Play 应用签名 - 存储上传密钥的推荐方式是什么?
- android - 在 Observer 中使用 AlarmManager 会导致仅调用一次 observe()
- javascript - 使用 htmlOutput 元素作为使用 JavaScript 导出的 PNG 文件的名称