[英]How to sort with nest order with Prisma
我有 2 张桌子
model Product {
id
name
skus[] Sku
}
model Sku {
id
name
price
weight
productId
product Product
}
数据示例:
id | name
--------------------
1 | product 1
2 | product 2
3 | product 3
id | name | price | weight | product_id
--------------------------------------------------
1 | sku 1 | 10 | 2 | 1
2 | sku 2 | 12 | 1 | 1
3 | sku 3 | 11 | 3 | 1
4 | sku 4 | 12 | 3 | 2
5 | sku 5 | 15 | 1 | 2
=>> 预期结果:
id | name
---------
2 | product 2
1 | product 1
现在,如果产品包含超过 1 个产品变体,我想按价格排序所有产品,采用 SKU 变体的最低价格。
注意:使用 prisma 查询
如果我理解你的问题是正确的(因为你想要的输出缺少 SKU id)在 prisma 的情况下你应该基本上 orderBy 关系和父母。
此外,如果您开始通过关系的某些属性(在这种情况下为 skus)进行查询,您应该记住在关系过滤条件中采用正确的“some”/“all”等,否则结果可能不是预期的,请参阅: https ://www.prisma.io/docs/concepts/components/prisma-client/relation-queries#relation-filters
prisma.Product.findMany({
include: {
skus: {
orderBy: {
price: 'asc'
},
// Remove to get all skus instead of the "cheapest"
take: 1
}
},
orderBy: {
id: 'desc'
}
})
在 prisma 上打开调试日志是个好主意: https ://www.prisma.io/docs/concepts/components/prisma-client/debugging
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.