Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: Cannot use SQL.Aliased from SubQuery in GroupBy #3632

Open
1 task done
Tracked by #3649
abegehr opened this issue Nov 27, 2024 · 0 comments
Open
1 task done
Tracked by #3649

[BUG]: Cannot use SQL.Aliased from SubQuery in GroupBy #3632

abegehr opened this issue Nov 27, 2024 · 0 comments
Labels
bug Something isn't working has-pr This issue has one or more PRs that that could close the issue when merged priority Will be worked on next qb/crud

Comments

@abegehr
Copy link

abegehr commented Nov 27, 2024

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.36.3

What version of drizzle-kit are you using?

0.28.1

Other packages

drizzle-zod@0.5.1

Describe the Bug

I'm unable to use a SQL.Aliased<number> from a subquery in the groupBy of a select. The ts error states that groupBy can only take columns:

Type 'Aliased<number>' is missing the following properties from type 'SQLiteColumn<ColumnBaseConfig<ColumnDataType, string>, object>': table, name, keyAsName, primary, and 17 more.ts(2769)

This is a simple sample, with a messages table (sender -> receiver), where I'm trying to get the latest message sent/received by the current user with all their friends. It fails on .groupBy(friendMessages.friendId) with the error above:

import * as t from "drizzle-orm/sqlite-core";
import { sqliteTable } from "drizzle-orm/sqlite-core";

// messages table for sending text messages sender->receiver (by id) at timestamp
const Message = sqliteTable("messages", {
  id: t.int().primaryKey(),
  ts: t.int({ mode: "timestamp" }),
  senderId: t.int().notNull(),
  receiverId: t.int().notNull(),
  content: t.text(),
});

// get list of all messages sent to and from the current user, return other user as friend
const friendMessages = db
  .select({
    id: Message.id,
    ts: Message.ts,
    friendId: sql<number>`CASE
                WHEN ${Message.senderId} = ${user.id} THEN ${Message.receiverId}
                ELSE ${Message.senderId}
                END`.as("friendId"),
  })
  .from(Message)
  .where(or(eq(Message.senderId, user.id), eq(Message.receiverId, user.id)))
  .as("friendMessages");

// group by friend uid to get the last message sent from/to the current user by each of their friends
const lastMessages = db
  .select()
  .from(friendMessages)
  .groupBy(friendMessages.friendId)
  .orderBy(desc(friendMessages.ts))
  .as("lastMessages");

If I try to group by one of the columns, like friendMessages.id, it works as expected. However, it doesn't allow me to group by the friendId, which is defined by the SQL-case in the subquery. Imo this would work in raw SQL.

@abegehr abegehr added the bug Something isn't working label Nov 27, 2024
@L-Mario564 L-Mario564 added priority Will be worked on next qb/crud labels Nov 28, 2024
@L-Mario564 L-Mario564 added the has-pr This issue has one or more PRs that that could close the issue when merged label Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working has-pr This issue has one or more PRs that that could close the issue when merged priority Will be worked on next qb/crud
Projects
None yet
Development

No branches or pull requests

2 participants