drizzle-team / drizzle-team/drizzle-orm

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

Open
#5,569 0 comments 0 reactions 0 assignees View on GitHub
1.0.0-beta.* bug db/postgres
Dominant language
TypeScript
Stars
35.8k
Forks
1.6k
Avg merge
2d 7h
Merged PRs (30d)
4

Description

### Report hasn't been filed before.

- [x] 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**
```ts
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')
})
);
```

```sh
> 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**
```ts
role: userRole().notNull().default(sql`'customer'::"user-role"`);
```

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

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.