Node.js 使用Prisma2和Jest删除表中的所有项目
我想知道如何使用Prisma2和Jest删除表中的所有项目 我读了这本书,我试着这样做: user.test.js 但我有一个错误:Node.js 使用Prisma2和Jest删除表中的所有项目,node.js,graphql,prisma,prisma-graphql,prisma2,Node.js,Graphql,Prisma,Prisma Graphql,Prisma2,我想知道如何使用Prisma2和Jest删除表中的所有项目 我读了这本书,我试着这样做: user.test.js 但我有一个错误: Invalid `prisma.user.deleteMany()` invocation: The change you are trying to make would violate the required relation 'PostToUser' between the `Post` and `User` models. 我的数据库 schema.p
Invalid `prisma.user.deleteMany()` invocation:
The change you are trying to make would violate the required relation 'PostToUser' between the `Post` and `User` models.
我的数据库
schema.prisma
您违反了
Post
和User
之间的外键约束。
在删除用户的帖子之前,不能删除该用户
beforeEach(async () => {
const prisma = new PrismaClient()
await prisma.post.deleteMany({where: {...}}) //delete posts first
await prisma.user.deleteMany({})
})
或设置,
这样,当您删除一个用户时,其帖子将自动删除这是另一种方法,这将删除所有行及其从属行,还将重置ID。通过这种方式,您可以迭代所有表,顺序并不重要
prisma.$executeRaw(`TRUNCATE TABLE ${table} RESTART IDENTITY CASCADE;`)
谢谢你的回答!这是Prisma 2的错误
model Post {
content String?
createdAt DateTime @default(now())
fk_user_id Int
id Int @default(autoincrement()) @id
published Boolean @default(false)
title String
author User @relation(fields: [fk_user_id], references: [id])
@@index([fk_user_id], name: "fk_user_id")
}
model User {
email String @unique
id Int @default(autoincrement()) @id
name String?
password String @default("")
Post Post[]
Profile Profile?
}
beforeEach(async () => {
const prisma = new PrismaClient()
await prisma.post.deleteMany({where: {...}}) //delete posts first
await prisma.user.deleteMany({})
})
prisma.$executeRaw(`TRUNCATE TABLE ${table} RESTART IDENTITY CASCADE;`)