drizzle-team / drizzle-team/drizzle-orm

[BUG]: Cannot use a dynamic select query as a subquery anymore (regression bug)

Open
#4,069 19 comments 21 reactions 1 assignee Claimed by @Sukairo-02 View on GitHub
bug priority qb/crud
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Report hasn't been filed before.

- [x] 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.39.1

### What version of `drizzle-kit` are you using?

0.30.4

### Other packages

_No response_

### Describe the Bug

## Steps to reproduce

Create a dynamic select query and call `.as` to pass it in as a subquery:

```typescript
const subquery = drizzle
.select({ id: schema.users.id })
.from(schema.users)
.$dynamic();

const query = drizzle
.select({ count: sql`cast(count(*) as int)` })
.from(subquery.as('__subquery__'))
// ^ Argument of type SubqueryWithSelection is not assignable to parameter of type 'TableLikeHasEmptySelection> extends true ? DrizzleTypeError<...> : SubqueryWithSelection<...>'.ts(2345)
```

This results in a TypeScript compile error:

```
Argument of type SubqueryWithSelection is not assignable to parameter of type 'TableLikeHasEmptySelection> extends true ? DrizzleTypeError<...> : SubqueryWithSelection<...>'.ts(2345)
```

This previously worked before and seems to have regressed when this was merged in:
- #3976

I still need to investigate if it's also broken in runtime. If not, the workaround is probably to just cast it as `any` for now.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.