![](/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.