索引字段 Prisma 删除外键约束失败

Aka*_*ash 4 postgresql prisma

当我尝试使用 postgres + prisma 从表中删除所有项目时,出现错误

\n

我有以下架构:

\n
model Profile {\n  id String @id @default(cuid())\n  projects Project[]\n}\n\nmodel Project {\n  id String @id @default(cuid())\n\n  profile Profile @relation(fields: [profile_email], references: [email], onDelete: NoAction)\n  profile_email String\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这是客户端的代码:

\n
model Profile {\n  id String @id @default(cuid())\n  projects Project[]\n}\n\nmodel Project {\n  id String @id @default(cuid())\n\n  profile Profile @relation(fields: [profile_email], references: [email], onDelete: NoAction)\n  profile_email String\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我得到的错误是这样的 -

\n
Invalid `prisma.profile.deleteMany()` invocation in\nclear.js:6:24\n\n  3 const prisma = new PrismaClient();\n  4 \n  5 (async () => {\n\xe2\x86\x92 6   await prisma.profile.deleteMany(\n  Foreign key constraint failed on the field: `Project_profile_email_fkey (index)`\n
Run Code Online (Sandbox Code Playgroud)\n

我们该如何解决这个问题呢?提前致谢。

\n

Ale*_*ing 6

当您尝试删除配置文件时,可能存在具有引用配置文件的外键的项目。由于这种关系,您在尝试删除配置文件时将会收到错误消息。您可以使用参考操作。在您的情况下,将项目模型中关系字段内的“OnDelete: NoAction 替换为 OnDelete: Cascade”。(编辑:确保使用新约束更新数据库)

您可以在这里阅读更多相关信息: 参考操作 | Prisma 文档