Ordering by relation in relational query

I am trying to sort on an attribute of a relation using the relational queries api, but as far as I can tell it is not supported? Example:
await db.query.city.findMany({
orderBy: (country , { asc }) => [asc(country.name)],
with: {
country: true,
},
});
await db.query.city.findMany({
orderBy: (country , { asc }) => [asc(country.name)],
with: {
country: true,
},
});
Thanks, J
2 Replies
roacfe
roacfe13mo ago
any updates on this? is it possible?
natedunn
natedunn13mo ago
it does work. worth noting that I think you might be confusing the tables that you are sorting. in your example country in orderBy is actually your city table. You might want to use orderBy in your with section. Here is an example of the difference. Note how I've named the tables:
columns: {...}
where: {...}
orderBy: (cityTable, { desc }) => desc(cityTable.name)
with: {
country: {
columns: {...}
where: {...}
orderBy: (countryTable, { desc }) => desc(countryTable.name)
}
}
columns: {...}
where: {...}
orderBy: (cityTable, { desc }) => desc(cityTable.name)
with: {
country: {
columns: {...}
where: {...}
orderBy: (countryTable, { desc }) => desc(countryTable.name)
}
}
Want results from more Discord servers?
Add your server