Prisma查询返回likes对象多余count属性,如何禁用?
问题
执行Prisma查询时,返回结果的likes对象中出现未请求的count属性,手动设置count: false也无法移除。对比发现Comment与Like模型结构几乎一致,仅Like带有@@unique([userId, postId])约束,怀疑该约束导致此问题。
查询代码
const user = await prisma.user.findUnique({ where: { id: id }, select: { id: true, username: true, posts: { omit: { userId: true, }, include: { comments: { select: { id: true, user: { select: { id: true, username: true, profilePicture: true, } }, text: true } }, likes: { select: { id: true, user: { select: { id: true, username: true, profilePicture: true, } } } } } } } })
返回结果示例
[ { "id": "cm29icdzk0003ljs4nbmvor5u", "user": { "id": "cm29ibc6h0001ljs4gr73xl6q", "username": "caio" }, "picture": "4cdd32cc-d2b3-4c26-9a1b-0f050ee7d48c", "comments": [ { "id": "cm2bbusv200002s8r14mk3s20", "text": "abc\n", "user": { "id": "cm29ibc6h0001ljs4gr73xl6q", "username": "caio", "profilePicture": "764d4cec-79e0-4aef-a7b7-af1c2642e37f" } }, { "id": "cm2bbvgwi00012s8rkkyvfgan", "text": "abc\n\n", "user": { "id": "cm29ibc6h0001ljs4gr73xl6q", "username": "caio", "profilePicture": "764d4cec-79e0-4aef-a7b7-af1c2642e37f" } }, { "id": "cm2bch34x00022s8rktrccf49", "text": "testando\n", "user": { "id": "cm29ibhas0002ljs4s50b4yoc", "username": "maria", "profilePicture": null } } ], "likes": { "users": [ { "id": "cm29ibc6h0001ljs4gr73xl6q", "username": "caio", "profilePicture": "764d4cec-79e0-4aef-a7b7-af1c2642e37f" }, { "id": "cm29ibhas0002ljs4s50b4yoc", "username": "maria", "profilePicture": null }, { "id": "cm29il7qe0004ljs4nujwiykh", "username": "gerson", "profilePicture": null } ], "count": 3 } } ]
模型定义
model Comment { id String @id @default(cuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt user User @relation(fields: [userId], references: [id]) userId String post Post @relation(fields: [postId], references: [id]) postId String text String @db.VarChar(500) } model Like { id String @id @default(cuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt user User @relation(fields: [userId], references: [id]) userId String post Post @relation(fields: [postId], references: [id]) postId String @@unique([userId, postId]) }
解决方案
原因分析
Like模型上的@@unique([userId, postId])约束会让Prisma自动推断出Post与User之间存在隐式多对多关系(通过Like表作为中间表)。此时查询Post.likes时,返回的是该多对多关系的聚合对象,默认包含关联用户列表users和计数count,而非Like记录的数组——这也是它和Comment的一对多关系返回结构不同的核心原因。
解决步骤
要获取Like记录的数组并移除count属性,需要显式定义Post与Like的一对多关系,覆盖Prisma的隐式多对多推断:
- 修改模型定义:在
Post和Like模型中显式命名一对多关系(假设你的Post模型已有基础字段):
model Post { id String @id @default(cuid()) // 其他字段(如user、picture等)... likes Like[] @relation("PostToLike") // 显式命名一对多关系 } model Like { id String @id @default(cuid()) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt user User @relation(fields: [userId], references: [id]) userId String post Post @relation("PostToLike", fields: [postId], references: [id]) // 关联到Post的显式关系 postId String @@unique([userId, postId]) }
- 重新生成Prisma客户端:执行命令更新客户端:
npx prisma generate
- 保持原查询代码不变:此时查询
Post.likes会返回Like记录的数组,不再包含count属性,结构与comments完全一致。
替代方案(无需修改模型)
如果不想修改模型,可以直接查询底层的一对多关联字段(Prisma为隐式多对多关系生成的底层字段,通常以_开头),替代原查询中的likes包含项:
// 原likes查询 likes: { select: { id: true, user: { select: { id: true, username: true, profilePicture: true, } } } } // 修改为 _likes: { select: { id: true, user: { select: { id: true, username: true, profilePicture: true, } } } }
注:_likes字段名可通过Prisma客户端的类型提示确认,不同版本可能略有差异。
内容的提问来源于stack exchange,提问作者Caio Henrique
相关产品推荐
相关产品推荐

