Order by on Joined Tables
I am attempting to do an order by for a joined table
I want to query
playerItem
with an order by on player.lastName
descending. I don't see any examples in the documentation on how to do this or if Drizzle allows it.
This is a simplified example of the schema, denormalizing would make sense here, but I purposely excluded the rest of the columns/relations to make reading easier
Any help would be appreciated!14 Replies
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
This will only order by the
player
table not the return of the playerItem
Essentially looking to order playerItem
based on player.lastName
Have you written anything? Show me the query you're using right now?
Im tyring to get a more basic example working. Im not using the tables mentioned above because my local db is not filled with that data yet.
My users table is
In the example above I want to return users ordered by their userRoleId
this would be very similar to the other tables above, but I want to order the parent by a relational table
@TOSL
{
"status": "error",
"error": "syntax error at or near "desc""
}
That query throws this error ^
Move the orderby inside the with
Or have you tried that and it isn't the output you're looking for?
I did try that and it only sorts the userRoleMappings
not the
users
I see.
Query can't order by related fields
at least not directly
you can use a subquery
or just use db.select()
Thats what I did get working here:
db.select().from(users).innerJoin(userRoleMappings,eq(users.id,userRoleMappings.userId)).orderBy(asc(userRoleMappings.userRoleId)).limit(5).execute()
Yeah this your best bet.
you shouldn't need execute() though
Im suprised they don't allow for the ordering by relation
figured it would be a common use case
:Shrug:
It's just not supported. The RQB using json aggregation to return the tables as arrays. It's just a limitation of how it was built.
I believe it something they intend to improve in relation api v2
@TOSL thanks for the help, looking forward to being able to use this in the future