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
23.54k stars 578 forks source link

[BUG]: Postgres Schema and Table name not properly escaped #2400

Closed deanvanniekerk closed 4 months ago

deanvanniekerk commented 4 months ago

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.21.4

Describe the Bug

Given a postgres schema and table name:

export const transactions = pgSchema("transactions");

export const TokenSwapInTransactions = transactions.table(
  "TokenSwap",
  {
    swapId: text("swapId").primaryKey().notNull(),
    transactionHash: text("transactionHash").notNull()
  }
);

when performing this query

const lastTokenSwapQuery = db.query.TokenSwapInTransactions.findFirst({
    orderBy: (tokenSwap, { desc }) => [desc(tokenSwap.logIndex)],
});

causes the following run time error

PostgresError: relation "transactions.TokenSwap" does not exist

the issue is that the schema and table name are not being correctly escaped

Expected behavior

Schema and Table names should be escaped in the format "{schema}"."{table}".

so in the example above "transactions.TokenSwap" should be "transactions"."TokenSwap"

Environment & setup

nodejs v20

deanvanniekerk commented 4 months ago

im so DUMB, I was pointing to the wrong db. This is not a bug.