Hey everyone, I have started with Prisma a few wee...
# orm-help
s
Hey everyone, I have started with Prisma a few weeks ago, so far I found it interesting I have a scenario where I want to order by nested relation For example, I have the following relation of one-to-many between item and variants
Copy code
model Item {
id Int
itemName String @map("item_name")
variants Variants[]
}

model Variants {
id Int
unitPrice Decimal @map("unit_price")
item Item
}
NOW I want to sort items by minimum price of any of its variant Is there a way I can achieve it via Prisma client, without using raw query to get my mapped fields in schema P.S: Prisma doesn’t provide option of other fields for nested relation apart from _count
m
Doesn't that work for you?
Copy code
await prisma.item.findMany({
  orderBy: {
    variants: {
      unitPrice: 'asc'
    }
  }
})
s
no it doesn’t provide any such option, I am only able to use _count here @Maciek K
m
Ok maybe that's only possible on the other side of relation: https://github.com/prisma/prisma/issues/5008#issuecomment-768444693
Would this save you:
Copy code
await prisma.variants.findMany({
  where: {
    itemId: yourItemid
  },
  orderBy: {
    unitPrice: 'asc'
  }
})
s
tried them but ain’t working @Maciek K
the last part is fetching via variants, while I need to fetch using the items table