drizzle-team / drizzle-team/drizzle-orm
[BUG]: Selecting from view results in undefined column names when the underlying tables are defined with column builder and name unspecified
- 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.44.5
### What version of `drizzle-kit` are you using?
^0.31.4
### Other packages
_No response_
### Describe the Bug
I will just dump my current table as an example of what is broken
Here is my table and view implementations
```
import { pgEnum, pgView } from "drizzle-orm/pg-core";
import { createTable } from "./utils";
import { sql, eq } from "drizzle-orm";
export const featureFlag = createTable("feature_flag", (d) => ({
id: d.integer().primaryKey().generatedByDefaultAsIdentity(),
name: d.varchar({ length: 256 }).notNull().unique(),
description: d.text(),
createdAt: d
.timestamp({ withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: d.timestamp({ withTimezone: true }).$onUpdate(() => new Date()),
}));
export const scopeEnum = pgEnum("scope", ["global", "user"]);
export type Scope = (typeof scopeEnum.enumValues)[number];
export const featureFlagSetting = createTable("feature_flag_setting", (d) => ({
id: d.integer().primaryKey().generatedByDefaultAsIdentity(),
featureFlagId: d
.integer()
.references(() => featureFlag.id)
.notNull(),
scope: scopeEnum().notNull(), // These become problematic since the are defined without name
entityId: d.text(), // These become problematic since the are defined without name
enabled: d.boolean().notNull(), // These become problematic since the are defined without name
createdAt: d
.timestamp({ withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: d.timestamp({ withTimezone: true }).$onUpdate(() => new Date()),
}));
export const featureFlagView = pgView("feature_flag_view").as((qb) =>
qb
.select({
id: featureFlag.id,
name: featureFlag.name,
description: featureFlag.description,
scope: featureFlagSetting.scope,
entityId: featureFlagSetting.entityId,
enabled: featureFlagSetting.enabled,
createdAt: featureFlagSetting.createdAt,
updatedAt: featureFlagSetting.updatedAt,
})
.from(featureFlag)
.leftJoin(
featureFlagSetting,
eq(featureFlag.id, featureFlagSetting.featureFlagId),
),
);
```
Then let say we select from the underlying `feature_flag_view` like this (NOTE even specifying each field manually wont work)
```
const sq = ctx.db
.select({
// id: featureFlagView.id,
// name: featureFlagView.name,
// description: featureFlagView.description,
// scope: featureFlagView.scope,
// entityId: featureFlagView.entityId,
// enabled: featureFlagView.enabled,
// createdAt: featureFlagView.createdAt,
// updatedAt: featureFlagView.updatedAt,
...getViewSelectedFields(featureFlagView),
})
.from(featureFlagView)
```
You will get some errors like this where it seems to try and select from undefined columns
```
...
Failed query: select "id", "name", "description", "undefined", "undefined", "undefined", "createdAt", "updatedAt", "flag_order" from (select "id", "name", "description", "undefined", "undefined", "undefined", "createdAt", "updatedAt",
...
```
This can be fixed by simply updating the schema. But in theory we should be able to do this without specifying the name
```
...
scope: scopeEnum("scope").notNull(),
entityId: d.text("entityId"),
enabled: d.boolean("enabled").notNull(),
...
```
Contributor guide
Assessment
This issue has not been assessed yet.