我正在嘗試從關注的用戶那里獲取所有帖子,以使以下用戶(又名我)不喜歡這些帖子。
我怎樣才能創建一個模式并查詢這樣的事情?
我目前有這個:
model User {
id String @id @default(autoincrement())
username String
followers Follows[] @relation("follower")
following Follows[] @relation("following")
likes Post[] @relation(references: [id])
}
model Follows {
follower User @relation("follower", fields: [followerId], references: [id])
followerId String
following User @relation("following", fields: [followingId], references: [id])
followingId String
@@id([followerId, followingId])
}
model Post {
id Int @id @default(autoincrement())
title String
likedBy User[] @relation(references: [id])
}
uj5u.com熱心網友回復:
架構中存在一些問題:
- 表中的關系
Follows似乎顛倒了,(即followerId應該映射到模型following上的關系User - 我假設每個帖子都是由用戶發布的,所以我在模型中添加了
author欄位Post autoincrement()使用Int型別,所以cuid()在User模型中使用
更新的架構:
model User {
id String @id @default(cuid())
username String
followers Follows[] @relation("follower")
following Follows[] @relation("following")
posts Post[] @relation("author")
likes Post[] @relation(references: [id])
}
model Follows {
follower User @relation("following", fields: [followerId], references: [id])
followerId String
following User @relation("follower", fields: [followingId], references: [id])
followingId String
@@id([followerId, followingId])
}
model Post {
id Int @id @default(autoincrement())
title String
authorId String
author User @relation("author", fields: [authorId], references: [id])
likedBy User[] @relation(references: [id])
}
要從關注的用戶那里獲取所有不喜歡的帖子:
const posts = await prisma.post.findMany({
where: {
likedBy: {
none: {
id: 'cl3g1s5a20011llvcsllnzlra', // same as followerId
},
},
author: {
followers: {
some: {
followerId: 'cl3g1s5a20011llvcsllnzlra',
},
},
},
},
});
some:回傳一個或多個(“一些”)相關記錄匹配過濾條件的所有記錄。
https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#some
none:回傳零相關記錄匹配過濾條件的所有記錄。- 您可以使用
none無引數回傳所有沒有關系的記錄
- 您可以使用
https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#none
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/479997.html
標籤:节点.js PostgreSQL 棱镜
上一篇:如何查詢加入@ManyToOnes表PostgreSQLSpringBootJPA
下一篇:是否需要在參考之前提交插入
