Prisma:select 多对多条件
Prisma : select on many to many with multiple conditions
我有两个 tableUser
和 Post
,它们通过多对多 table 的习惯链接,例如:
model User {
id Int @id @default(autoincrement())
name String
enabled Bool
posts users_to_posts[]
}
model Post {
id Int @id @default(autoincrement())
name String
enabled Bool
users users_to_posts[]
}
model user_to_post {
user user? @relation(fields: [user_id], references: [id])
user_id Int
post post? @relation(fields: [post_id], references: [id])
post_id Int
@@id([user_id, post_id])
}
我正在尝试根据 post ID 列表获取用户列表,其中必须启用用户和 posts。
到目前为止,如果他们在给定的 post 数组中有 post,我可以获得已启用的正确用户,但我无法检查 post 是否已启用或不能,我也不能过滤 posts(如果有匹配,我会得到与用户关联的所有 posts)
这是我的(几乎)工作代码:
import { PrismaClient, Prisma } from '@prisma/client'
const prisma = new PrismaClient()
export default async function handler(req, res) {
if (req.method !== 'POST') {
res.status(400).send({ message: 'Only POST requests allowed for this route' })
} else {
const { posts_id } = req.query
const posts_array = posts_id.split(",").map(function(item) {
return parseInt(item)
})
const ret = await prisma.user.findMany({
where: {
enabled: true,
post: { some: { post_id: { in: posts_array } }, },
},
include: {
_count: { select: { post: true } }
post: { select: { post: true }, },
},
})
res.status(200).send(ret)
// ...
}
}
我仍在努力了解如何在不依赖打字稿的情况下进行多个嵌入式选择以使查询正常工作( 远非理想)
据我了解,您需要的 2 个约束条件目前没有出现在您的查询中。
- 只有 return 一个
user
如果 posts_array
里面的 post 被启用。
- 过滤
user
的 returned post,使其只包含 enabled
post。
我已更新您的查询以添加这两个条件。
const users = await prisma.user.findMany({
where: {
enabled: true,
posts: {
some: {
post_id: { in: posts_array },
post: {
enabled: true // for constraint 1 (only check/match against the post_ids in post array which are enabled)
}
},
},
},
include: {
_count: { select: { posts: true } },
posts: {
select: { post: true },
where: {
post: {
enabled: true // for constraint 2 (only include the posts which are enabled)
}
}
},
},
})
请记住,users[SOME_IDX]._count.posts
将包含该用户所有 post 的计数(包括那些 disabled
)。如果您只想计算启用的 posts,则必须检查 users[SOME_IDX].posts
数组的长度。
顺便说一下,根据您的架构,user_to_post
table 有点多余。您可以使用 implicit many-to-many 来模拟 post 和用户之间的关系。
我有两个 tableUser
和 Post
,它们通过多对多 table 的习惯链接,例如:
model User {
id Int @id @default(autoincrement())
name String
enabled Bool
posts users_to_posts[]
}
model Post {
id Int @id @default(autoincrement())
name String
enabled Bool
users users_to_posts[]
}
model user_to_post {
user user? @relation(fields: [user_id], references: [id])
user_id Int
post post? @relation(fields: [post_id], references: [id])
post_id Int
@@id([user_id, post_id])
}
我正在尝试根据 post ID 列表获取用户列表,其中必须启用用户和 posts。
到目前为止,如果他们在给定的 post 数组中有 post,我可以获得已启用的正确用户,但我无法检查 post 是否已启用或不能,我也不能过滤 posts(如果有匹配,我会得到与用户关联的所有 posts)
这是我的(几乎)工作代码:
import { PrismaClient, Prisma } from '@prisma/client'
const prisma = new PrismaClient()
export default async function handler(req, res) {
if (req.method !== 'POST') {
res.status(400).send({ message: 'Only POST requests allowed for this route' })
} else {
const { posts_id } = req.query
const posts_array = posts_id.split(",").map(function(item) {
return parseInt(item)
})
const ret = await prisma.user.findMany({
where: {
enabled: true,
post: { some: { post_id: { in: posts_array } }, },
},
include: {
_count: { select: { post: true } }
post: { select: { post: true }, },
},
})
res.status(200).send(ret)
// ...
}
}
我仍在努力了解如何在不依赖打字稿的情况下进行多个嵌入式选择以使查询正常工作( 远非理想)
据我了解,您需要的 2 个约束条件目前没有出现在您的查询中。
- 只有 return 一个
user
如果posts_array
里面的 post 被启用。 - 过滤
user
的 returned post,使其只包含enabled
post。
我已更新您的查询以添加这两个条件。
const users = await prisma.user.findMany({
where: {
enabled: true,
posts: {
some: {
post_id: { in: posts_array },
post: {
enabled: true // for constraint 1 (only check/match against the post_ids in post array which are enabled)
}
},
},
},
include: {
_count: { select: { posts: true } },
posts: {
select: { post: true },
where: {
post: {
enabled: true // for constraint 2 (only include the posts which are enabled)
}
}
},
},
})
请记住,users[SOME_IDX]._count.posts
将包含该用户所有 post 的计数(包括那些 disabled
)。如果您只想计算启用的 posts,则必须检查 users[SOME_IDX].posts
数组的长度。
顺便说一下,根据您的架构,user_to_post
table 有点多余。您可以使用 implicit many-to-many 来模拟 post 和用户之间的关系。