Prisma 不喜欢关注用户的帖子
Prisma not liked posts from followed users
我正在尝试从关注的用户那里获取所有 帖子,以便这些帖子未被以下用户(又名我)点赞。
我如何创建模式并查询这样的东西?
我目前有这个:
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])
}
架构中存在一些问题:
Follows
table 中的关系似乎颠倒了,(即 followerId
应该映射到 User
模型上的 following
关系
- 我假设每个 post 都是由用户制作的,所以我在
Post
模型中添加了 author
字段
autoincrement()
使用 Int
类型,因此在 User
模型 中使用 cuid()
更新架构:
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])
}
要从关注的用户那里获得所有不喜欢的 post:
const posts = await prisma.post.findMany({
where: {
likedBy: {
none: {
id: 'cl3g1s5a20011llvcsllnzlra', // same as followerId
},
},
author: {
followers: {
some: {
followerId: 'cl3g1s5a20011llvcsllnzlra',
},
},
},
},
});
some
:Returns 一条或多条(“一些”)相关记录符合过滤条件的所有记录。
https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#some
none
: Returns 零个相关记录符合过滤条件的所有记录。
- 您可以使用不带参数的
none
来return所有没有关系的记录
https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#none
我正在尝试从关注的用户那里获取所有 帖子,以便这些帖子未被以下用户(又名我)点赞。
我如何创建模式并查询这样的东西?
我目前有这个:
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])
}
架构中存在一些问题:
Follows
table 中的关系似乎颠倒了,(即followerId
应该映射到User
模型上的following
关系- 我假设每个 post 都是由用户制作的,所以我在
Post
模型中添加了author
字段 autoincrement()
使用Int
类型,因此在User
模型 中使用
cuid()
更新架构:
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])
}
要从关注的用户那里获得所有不喜欢的 post:
const posts = await prisma.post.findMany({
where: {
likedBy: {
none: {
id: 'cl3g1s5a20011llvcsllnzlra', // same as followerId
},
},
author: {
followers: {
some: {
followerId: 'cl3g1s5a20011llvcsllnzlra',
},
},
},
},
});
some
:Returns 一条或多条(“一些”)相关记录符合过滤条件的所有记录。
https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#some
none
: Returns 零个相关记录符合过滤条件的所有记录。- 您可以使用不带参数的
none
来return所有没有关系的记录
- 您可以使用不带参数的
https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#none