drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.44k stars 641 forks source link

[BUG]:Drizzle ORM not working with view #2850

Open codemaverick80 opened 2 months ago

codemaverick80 commented 2 months ago

What version of drizzle-orm are you using?

0.32.1

What version of drizzle-kit are you using?

0.23.1

Describe the Bug

I have an existing view in my PostgreSQL database and its very simple view. I am trying to query this view from my Nextjs app with drizzle orm but I am getting error.

Unhandled Runtime Error Error: syntax error at or near "$1"

Here is how i am declaring and using it.

schema.ts


export const accountAddressView = pgView("acc_address_view",{
  id:serial("id"),
  accountId: integer("account_id"),
  name: text("name"),
  address: text("address"),
  city: text("city"),
}).existing();

action.ts

export async function getAccountInfo(){

     const data= await db.select().from(accountAddressView);
     return data;

}

when i run the app i am getting

chrome_2FtyUReP7m

I also checked Postgres log and this is what i see

2024-08-26 21:35:33.057 PDT [1860] STATEMENT:  select  from $1
2024-08-26 21:36:03.324 PDT [3744] ERROR:  syntax error at or near "$1" at character 14

I am able to query view in PgAdmin without any issue.

SELECT * FROM acc_address_view

Expected behavior

should return data from postgres view

Environment & setup

OS: windows 10 64 bit

Node Version: v20.11.1

NextJs version: 14.2.5

PatrickE94 commented 1 week ago

Seeing the same on drizzle-orm 0.33.0.

Looking at the PSQL logs the table name is replaced with $1. E.g. select from $1 where "test_view"."user_id" = $2. Also note that my selection seems to have disappeared.

PatrickE94 commented 1 week ago

I spent 20 minutes on it. Placing

console.log('PG_VIEW_BASE', is(table, PgViewBase));

in the beginning of buildSelectQuery in dialect.js shows that as long as the query works (first load for me in Next.js), it returns true. On a reload it starts showing false and fails.

It only manifests for me in dev-mode. In release mode it runs fine. But having to restart in continuously in dev really hampers the flow...