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
21.47k stars 486 forks source link

[BUG]:SQL index against reserved column names omit quotes #2484

Open Aymericr opened 3 weeks ago

Aymericr commented 3 weeks ago

What version of drizzle-orm are you using?

0.31.1

What version of drizzle-kit are you using?

0.22.2

Describe the Bug

Notice the use of "user" as a column name:

export const customers = pgTable(
  "customers",
  {
    id: id("customer"),
    user: text("user").references(() => users.id, {
      onDelete: "set null",
    }),
    organization: text("organization")
      .notNull()
      .references(() => organizations.id, { onDelete: "cascade" }),
  },
  (table) => ({
    customer_user_organization_idx: uniqueIndex().on(
      table.user,
      table.organization,
    ),
  }),
)

Generates the following SQL:

CREATE UNIQUE INDEX IF NOT EXISTS "customers_user_organization_index" ON "customers" USING btree (user,organization);--> statement-breakpoint

Which yield the following error:

ERROR:  functions in index expression must be marked IMMUTABLE

Expected behavior

Should generate the following SQL:

CREATE UNIQUE INDEX IF NOT EXISTS "customers_user_organization_index" ON "customers" USING btree ("user",organization);--> statement-breakpoint

Environment & setup

No response