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.44k stars 484 forks source link

[BUG]: .default() does not work on boolean pg types #2559

Open codesfromshad opened 3 days ago

codesfromshad commented 3 days ago

What version of drizzle-orm are you using?

0.31.2

What version of drizzle-kit are you using?

0.22.7

Describe the Bug

When I set .default(sql'FALSE').notNull() or .default(false).notNull(), manually entering a row does not result in FALSE by default.

Check the snapshot: image

The CHECK() constraint, using sql magic operator also doesn't work. I am aware the drizzle did not implement this yet.

Please check the code snippet below for understanding what I am trying to achieve:

export const trackers = createTable(
  "tracker",
  {
    id: uuid("id").primaryKey(),
    createdAt: timestamp("created_at", {
      mode: "date",
      withTimezone: true,
    }).default(sql`CURRENT_TIMESTAMP`).notNull(),
    hashedId: varchar("hashed_id", { length: 255 }).notNull(),
    country: varchar("country", { length: 2 }).notNull(),
    isMalicious: boolean("is_malicious").default(sql`FALSE`).notNull(),
    canBypassProtection: boolean("can_bypass_protection").default(sql`FALSE`).notNull(),
  },
  (tracker) => ({
    hashedIdIdx: index("tacker_hashedId_idx").on(tracker.hashedId),
    canBypassProtectionCstr:
      sql`CHECK (can_bypass_protection = FALSE OR (can_bypass_protection = TRUE AND is_malicious = TRUE))`, // <-- DOESN'T WORK
  })
);

Expected behavior

It should be FALSE by default.

Environment & setup

No response