drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: Postgres customType generate invalid SQL.

Open cdslxc opened this issue 2 years ago • 0 comments

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.

cdslxc avatar Mar 30 '24 11:03 cdslxc