Skip to content

[BUG]: false positve detection of schema changes for default values #5569

@damaafer

Description

@damaafer

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

1.0.0-beta.20

What version of drizzle-kit are you using?

1.0.0-beta.20

Other packages

No response

Describe the Bug

database driver
postgres@3.4.8

What is the undesired behavior?
drizzle-kit push falsely detects schema changes for default values.
It compare the value retrieved from the database which contains type casting with the raw value without type casting.

What are the steps to reproduce it

import { pgEnum, pgTable } from "drizzle-orm/pg-core";

export const userRole = pgEnum('user-role', ['admin', 'operator', 'customer']);

export const user = pgTable(
  'user',
  (t) => ({
    id: t.integer().primaryKey().generatedAlwaysAsIdentity(),
    role: userRole().notNull().default('customer')
  })
);
> drizzle-kit push
[✓] Pulling schema from database...

┌─── public.user.role column changed:
│ default: 'customer'::"user-role" -> 'customer'
├───
│ ALTER TABLE "user" ALTER COLUMN "role" SET DEFAULT 'customer'::"user-role";
└───

ALTER TABLE "user" ALTER COLUMN "role" SET DEFAULT 'customer'::"user-role";

Simple workaround

role: userRole().notNull().default(sql`'customer'::"user-role"`);

Typesafe workaroud

//db-utils.ts
import { sql } from 'drizzle-orm';
import { pgEnum } from 'drizzle-orm/pg-core';

export function enumDefault<EnumValues extends readonly [string, ...string[]]>(
  enumType: ReturnType<typeof pgEnum<string, EnumValues>>,
  defaultValue: EnumValues[number]
) {
  return sql.raw(`'${defaultValue}'::"${enumType.enumName}"`
  );
}
role: userRole().notNull().default(enumDefault(userRole, 'customer'));

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions