drizzle-team / drizzle-team/drizzle-orm
[FEATURE]: Allow orderBy for relational queries to contain joined tables
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Describe what you want
It may be the easiest showing you an example. I want to do this:
```js
db.query.members.findMany({
where: eq(members.clubId, 'clubId'),
orderBy: [asc(members.role), asc(users.firstname), asc(users.lastname)],
with: { user: true },
});
```
But `asc(users.firstname), asc(users.lastname)` is not allowed there, therefore I currently need to write:
```js
db
.select()
.from(members)
.innerJoin(users, eq(members.userId, users.id))
.where(eq(members.clubId, 'clubId'))
.orderBy(asc(members.role), asc(users.firstname), asc(users.lastname));
```
Schema definition:
```js
export const users = pgTable('users', {
id: text('id').primaryKey(),
firstname: text('firstname').notNull(),
lastname: text('lastname').notNull(),
});
export const usersRelations = relations(users, ({ many }) => ({
members: many(members),
}));
export const members = pgTable('members', {
id: serial('id').primaryKey(),
clubId: text('club_id').notNull(),
userId: text('user_id').notNull().references(() => users.id),
role: text('role').notNull(),
});
export const membersRelations = relations(members, ({ one }) => ({
user: one(users, {
fields: [members.userId],
references: [users.id],
}),
}));
```
Is there any reason, why this is currently not supported? I would love to see this feature in drizzle to use relational queries more often :)
Contributor guide
Research direction
Start at the relational query findMany entry point and its orderBy handling, using the provided members/users example to trace how joined-table columns are validated. Done means relational queries accept asc(users.firstname) and asc(users.lastname) alongside local columns and produce the intended ordering; no file or test path is named in the issue.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- postgresql, typescript
- Domain
- databases
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100