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]: Postgres customType generate invalid SQL. #2087

Open cody-ta opened 7 months ago

cody-ta commented 7 months ago

What version of drizzle-orm are you using?

0.30.6

What version of drizzle-kit are you using?

0.20.14

Describe the Bug

//schema.ts
const isnotnullcheck = customType<{
  data: boolean;
  config: {
    columns: string[];
  };
}>({
  fromDriver(value) {
    return Boolean(value);
  },
  dataType(config) {
    if (!config) throw new Error("");

    return `boolean GENERATED ALWAYS AS (${config.columns
      .map(
        (value, idx, arr) =>
          `(${value} IS NOT NULL)${idx < arr.length - 1 ? " AND " : ``}`,
      )
      .join("")}) STORED`;
  },
});

export const memos = pgTable(
  "memos",
  {
    id: bigint("id", { mode: "number" }).primaryKey(),
    sender: varchar("sender", { length: 20 }).notNull(),
    recipient: varchar("recipient", { length: 20 }),
    isReceived: isnotnullcheck("isReceived", {
      columns: ["recipient"],
    }),
  },
);

After I generate via drizzle-kit pg:generate, I was unable to migrate.

--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "memos" (
    "id" bigint PRIMARY KEY NOT NULL,
    "sender" varchar(20) NOT NULL,
    "recipient" varchar(20),
    "isReceived" "boolean GENERATED ALWAYS AS ((recipient IS NOT NULL)) STORED"
);

It should be "isReceived" boolean GENERATED ALWAYS AS ((recipient IS NOT NULL)) STORED (without quote ").

Expected behavior

Should generate valid SQL.

Environment & setup

I use Vercel Postgres.

curiosbasant commented 2 months ago

Its a long-standing bug; when would it be looked into?