hello guys , i have a quetion about sorting in relational field in prisma , i have the below models:

hello guys , i have a quetion about sorting in relational field in prisma , i have the below models: model speciality { id String @id @default(uuid()) medical_Files medical_File[] medicalFileTemplate medical_File_templates[] institutionUsers institutionUsers[] created_at DateTime @default(now()) updated_at DateTime? @updatedAt InstitutionExistingSpecialities InstitutionExistingSpecialities[] InviteNewUsers InviteNewUsers[] joinInstitutionRequests joinInstitutionRequests[] collaborationSpeciality collaborationSpeciality[] selectListSpeciality selectListSpeciality[] specialityLabel specialityLabel[] } model expertiseLabel { id String @id @default(uuid()) label String language availableLanguages expertise expertise @relation(fields: [expertiseId], references: [id], onDelete: Cascade) expertiseId String priority Int @default(0) created_at DateTime @default(now()) updated_at DateTime? @updatedAt @@unique([expertiseId, language]) } and i have the below code in nest in order to fetch speciality and sort them by label : 'asc' of specialityLabel const specialities = await this.prisma.speciality.findMany({ orderBy: { specialityLabel: { label: 'asc' } }, }); i got this error : error TS2353: Object literal may only specify known properties, and 'label' does not exist in type 'specialityLabelOrderByRelationAggregateInput'. is there any solution that help me sort speciality by label of specialityLabel ?
4 Replies
RaphaelEtim
RaphaelEtim7d ago
Hi @Rami kadaweh Can you try this query?
const specialities = await prisma.speciality.findMany({
include: {
specialityLabel: {
orderBy: {
label: 'asc'
}
}
}
});
const specialities = await prisma.speciality.findMany({
include: {
specialityLabel: {
orderBy: {
label: 'asc'
}
}
}
});
Rami kadaweh
Rami kadawehOP7d ago
don't give me a good result because in this way specialityLabel are ordered by label inside secific speciality . the result that i want to order speciality by label of specialityLabel
RaphaelEtim
RaphaelEtim7d ago
Hi @Rami kadaweh There isn't a straightforward way to order specialities by the label of specialityLabel using the standard query API because Prisma doesn't support ordering by fields in one-to-many relations in the way you need. You can see this open issue and this other issue that mentions ordering by one-to many fields. The workaround would be to use a raw SQL query to achieve the desired sorting.
GitHub
Support for deep orderBy sorting / querying · Issue #5837 · prism...
Problem Considering the following schema (highly simplified): model Item { id Int @id @default(autoincrement()) @db.Int() localization ItemI18n[] } model ItemI18n { id Int @id @default(autoincremen...
GitHub
Not able to orderBy on one-to-many fields · Issue #21174 · prisma...
Problem Hi everyone, I'm working on an API which relies on internationalization. I'm trying to order data by name alphabetically, but this use case seems to not be possible with Prisma. Her...
Rami kadaweh
Rami kadawehOP7d ago
thank you @RaphaelEtim
Want results from more Discord servers?
Add your server