drizzle-team / drizzle-team/drizzle-orm

[BUG]: Column names in `pgView` definitions are not used

Open
#3,202 4 comments 5 reactions 1 assignee Claimed by @L-Mario564 View on GitHub
drizzle/kit improvement
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

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

0.35.3

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

0.26.2

### Describe the Bug

1. Write the following schema

```ts
import { eq } from "drizzle-orm";
import { pgTable, uuid, text, pgView } from "drizzle-orm/pg-core";

export const table1 = pgTable("table_1", {
id: uuid().primaryKey().defaultRandom(),
foo: text(),
});

export const table2 = pgTable("table_2", {
id: uuid().references(() => table1.id),
foo: text(),
});

export const view = pgView("view").as((qb) =>
qb
.select({ id: table1.id, foo_one: table1.foo, foo_two: table2.foo })
.from(table1)
.leftJoin(table2, eq(table1.id, table2.id)),
);
```

2. Generate migration files with `drizzle-kit` (you can also use https://drizzle.run/converter)

3. The SQL that generates the view looks like

```sql
CREATE VIEW "public"."view" AS (
select
"table_1"."id",
"table_1"."foo",
"table_2"."foo"
from "table_1"
left join "table_2" on "table_1"."id" = "table_2"."id"
);
```

This generates a view with two columns named `foo`, which will error when trying to apply the migration.

### Expected behavior

The columns in the view should take the name of the properties in the `qb.select()`, and the select in the `CREATE VIEW` should look say `table_1.foo AS foo_one, table_2.foo AS foo_two`.

### Environment & setup

https://drizzle.run/converter

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.