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]: "undefined" column from a materialized view #3856

Open
1 task done
roxizhauk opened this issue Dec 28, 2024 · 0 comments
Open
1 task done

[BUG]: "undefined" column from a materialized view #3856

roxizhauk opened this issue Dec 28, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@roxizhauk
Copy link

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.38.2

What version of drizzle-kit are you using?

0.30.1

Other packages

No response

Describe the Bug

What is the undesired behavior?
The query to select from a materialized view won't be generated properly.

What are the steps to reproduce it?
This materialized view is generated properly as I could confirmed in raw PostgreSQL.

export const filteredItems = pgMaterializedView("filtered_items").as((qb) =>
  qb
    .select({
      id: items.id,
      name: items.name,
      status: items.status,
      currentPrice: items.currentPrice,
      originalPrice: items.originalPrice,
      itemUrl: items.itemUrl,
      sharedLink: items.sharedLink,
      title: titles.title,
    })
    .from(items)
    .leftJoin(titles, eq(titles.id, items.titleId))
    .where(eq(items.hasImg, true))
    .orderBy(desc(items.createdAt)),
);

However, when querying a simple selection as below, it won't work because title column gets undefined in the generated query when either of id, name or status is selected with it.

  const query = db
    .select({
      id: filteredItems.id,
      name: filteredItems.name,
      status: filteredItems.status,
      title: filteredItems.title,
    })
    .from(filteredItems)
    .limit(1);
  console.log(query.toSQL());

Here's the generated query.

{
  sql: 'select "id", "name", "status", "undefined" from "filtered_items" limit $1',
  params: [ 1 ]
}

But title can be back when removing those three columns.

{ sql: 'select "title" from "filtered_items" limit $1', params: [ 1 ] }

The current workaround is replace title: filteredItems.title with title: sql<string>`title`.

What is the desired result?
All columns are to be selected at once without workaround.

@roxizhauk roxizhauk added the bug Something isn't working label Dec 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant