![](/img/trans.png)
[英]Can't make two 1:1 relations in one model in Prisma. Ambiguous relation detected
[英]How to made a relation to user model using Prisma?
我正在尝试查询评论和发表评论的用户。 我正在使用 Prisma 创建架构并将 Planetscale 作为数据库。
您可以在下面找到我的架构的一部分
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
Comment Comment[]
}
model Comment {
id String @id @default(cuid())
text String
contentId String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
commenterId String?
commenter User? @relation(fields: [commenterId], references: [id])
}
这是我的 API 路线:
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "../../../lib/prisma";
export default async function handle(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "GET") {
return res.status(405).json({ msg: "Method not allowed" });
}
try {
const comments = await prisma.comment.findMany();
return res.status(200).json(comments);
} catch (err) {
console.error(err);
res.status(500).json({ msg: "Something went wrong." });
}
res.end();
}
最终目标是查询评论并获得带有评论者的 object,这样我就可以显示名称和图像。
我在 model 评论中的@relation 应该如何使查询中包含评论者成为可能?
如果要包含相关的 model ,则需要告诉 prisma,如下所示:
await prisma.comment.findmany({
include: {
commenter: true,
},
})
https://www.prisma.io/docs/concepts/components/prisma-client/relation-queries
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.