这是我的 datamodel.prisma 文件的相关部分。
type Driver {
id: ID! @unique
zones: [Zone!] @relation(name: "DriverZones")
shifts: [Shift!] @relation(name: "DriverShifts")
preferredZone: Zone
preferredShift: Shift
}
type Shift {
id: ID! @unique
drivers: [Driver! ] @relation(name: "DriverShifts")
}
type Zone {
id: ID! @unique
drivers: [Driver! ] @relation(name: "DriverZones")
}
Run Code Online (Sandbox Code Playgroud)
在这里,我想根据我创建的数据模型为preferredZone 和preferredShift 创建类型为Zone 和Shift 的关系。这是一种单向关系。
关系字段preferredShift必须指定一个@relation指令:@relation(name: "MyRelation")
,关系字段preferredZone必须指定一个@relation指令:@relation(name: "MyRelation")
我的 prisma 数据库使用 PostgreSQL。如何建立preferredZone到Zone之间的关系。并将首选Shift 改为Shift。
@scalarList在运行pyramida部署后,程序会弹出->(策略参数的有效值是:RELATION。)。谁知道为什么?
type User {
id: ID! @id
name: String!
email: String! @unique
password: String!
age: Int
img: String
location: Location
hostedEvents: [Event]! @relation(name: "HostedEvents", onDelete: CASCADE)
joinedEvents: [Event]! @relation(name: "EventMembers", onDelete: CASCADE)
pushNotificationTokens: [PushNotificationTokens]!
createdAt: DateTime! @createdAt
updatedAt: DateTime! @updatedAt
}
Run Code Online (Sandbox Code Playgroud)
type Event {
id: ID! @id
owner: User! @relation(name: "HostedEvents")
name: String!
imgs: [String]!
description: String
start: DateTime!
end: DateTime!
categories: [Category]!
members: [User]! @relation(name: "EventMembers")
chatRoom: GroupChatRoom!
pendingRequests: [PendingRequest]!
locations: [Location]!
comments: [Comment]!
createdAt: DateTime! @createdAt
updatedAt: …Run Code Online (Sandbox Code Playgroud) 我想知道如何使用 Prisma2 和 Jest 删除表中的所有项目?
我阅读了CRUD 文档并尝试这样做:
用户.test.js
....
import { PrismaClient } from "@prisma/client"
beforeEach(async () => {
const prisma = new PrismaClient()
await prisma.user.deleteMany({})
})
...
Run Code Online (Sandbox Code Playgroud)
但我有一个错误:
Invalid `prisma.user.deleteMany()` invocation:
The change you are trying to make would violate the required relation 'PostToUser' between the `Post` and `User` models.
Run Code Online (Sandbox Code Playgroud)
我的数据库
CREATE TABLE User (
id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL,
name VARCHAR(255),
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE TABLE Post …Run Code Online (Sandbox Code Playgroud) 我想更新一个实体的firstName和。lastNameprofile
我希望用户能够更新它们两者或仅更新其中之一。
但是我不知道如何进行突变,使得参数 ( firstNameand lastName) 之一是可选的。
我的当前代码如果用户同时输入firstName和lastName
@Mutation(() => Boolean)
@UseMiddleware(isAuth)
async updateProfile(
@Ctx() {payload}: MyContext,
@Arg('firstName') firstName: string,
@Arg('lastName') lastName: string,
) {
try {
const profile = await Profile.findOne({where: { user: payload!.userId}})
if (profile) {
profile.firstName = firstName
profile.lastName = lastName
await profile.save();
return true
}
return false
} catch(err) {
return false
}
}
Run Code Online (Sandbox Code Playgroud)
如果我运行突变(不包括一个参数):
mutation{
updateProfile(firstName: "test")
}
Run Code Online (Sandbox Code Playgroud)
我收到错误:
"message": "String!" 类型的字段 "updateProfile" 参数 …
我被迷惑了。我最初创建了用户查询,它给了我错误,我认为是语法错误。但后来我创建了一个相同的车辆查询,效果非常好。我怀疑这和ID有关!类型,但我已经没有线索了。任何帮助,将不胜感激!
这是我的类型定义和解析器。
//类型定义//
type User {
id: ID!
fname: String
lname: String
email: String
password: String
vehicles: [Vehicle]
}
type Vehicle {
id: ID!
vin: String
model: String
make: String
drivers: [User]
}
type Query {
users: [User]
user(id: ID!): User
vehicles: [Vehicle]
vehicle(vin: String): Vehicle
}
Run Code Online (Sandbox Code Playgroud)
//解析器//
user: async (parent, args, context) => {
const { id } = args
return context.prisma.user.findUnique({
where: {
id,
},
})
},
vehicle: async (parent, args, context) => { …Run Code Online (Sandbox Code Playgroud) 我在 Postgres 中使用 Prisma 1.9。
我怎样才能重置一切?我试过了,prisma local nuke但是这个命令会添加 MySQL 容器(以某种方式),然后它会抛出端口4466正在使用的错误......
我还尝试重新创建所有 docker 容器和图像,但这也无济于事。
那么,正确的方法是什么?prisma reset只删除数据,但我也想删除架构..我想把它全部擦掉。
我刚刚学习了如何使用graphql-yoga并prisma-binding基于HowToGraphQL 教程来创建 GraphlQL 服务器。
问题:目前查询数据库的唯一方法是使用通过运行命令启动的 Prisma Playground 网页graphql playground。
是否可以从 Node.js 脚本执行相同的查询?我遇到了 Apollo 客户端,但它似乎是为了从 React、Vue、Angular 等前端层使用。
type Category {
id: ID! @id
name: String!
}
type SubCategoryLevel1 {
id: ID! @id
name: String!
parentCategory: Category! @relation(link: INLINE)
}
type SubCategoryLevel2 {
id: ID! @id
name: String!
parentCategory: SubCategoryLevel1! @relation(link: INLINE)
}
Run Code Online (Sandbox Code Playgroud)
如果我的Category水平没有确定怎么办,我正在使用Prisma ORM和MongoDB.
在我的 Prisma 模式中,帖子和类别之间存在多对多关系。我添加了@map选项以匹配 Postgres snake_case 命名约定:
model Post {
id Int @id @default(autoincrement())
title String
body String?
categories PostCategory[]
@@map("post")
}
model Category {
id Int @id @default(autoincrement())
name String
posts PostCategory[]
@@map("category")
}
model PostCategory {
categoryId Int @map("category_id")
postId Int @map("post_id")
category Category @relation(fields: [categoryId], references: [id])
post Post @relation(fields: [postId], references: [id])
@@id([categoryId, postId])
@@map("post_category")
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试同时创建一个包含多个类别的帖子。如果存在类别,我想connect将类别添加到帖子中。如果该类别不存在,我想创建它。创建部分运行良好,但连接部分有问题:
await prisma.post.create({
data: {
title: 'Hello',
categories: {
create: [{ category: { create: { name: 'News' …Run Code Online (Sandbox Code Playgroud) prisma-graphql ×10
graphql ×8
prisma ×8
prisma2 ×3
apollo ×1
mongodb ×1
node-modules ×1
node.js ×1
reactjs ×1
typegraphql ×1
typescript ×1