drizzle-team / drizzle-team/drizzle-orm

[BUG]: When renaming a varchar column and changing its length, drizzle generate only detect the name change and ignore the length change

Open
#5,499 1 comment 0 reactions 0 assignees View on GitHub
bug bug/fixed-in-beta
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?

0.45.1

### What version of `drizzle-kit` are you using?

0.31.8

### Other packages

_No response_

### Describe the Bug

When renaming a varchar column and changing its length, drizzle generate only detect the name change and ignore the length change.

# How to reproduce

Create a schema like below

```ts
export const posts = pgTable(
'posts',
{
id: uuid('id').primaryKey().defaultRandom(),
name: varchar('name', { length: 255 }).notNull(),
}
)
```

Migrate with `drizzle-kit generate` & `drizzle-kit migrate`

Edit the schema.(Rename name to title and remove the length)

```ts
export const posts = pgTable(
'posts',
{
id: uuid('id').primaryKey().defaultRandom(),
title: varchar('title').notNull(),
}
)
```

Migrate with `drizzle-kit generate` & `drizzle-kit migrate`

Generated migration file

```sql
ALTER TABLE "posts" RENAME COLUMN "name" TO "title";
```

No length changes

Snapshot file

```json
{
"name": "user_api_tokens",
"schema": "",
"columns": {
"id": {
"name": "id",
"type": "uuid",
"primaryKey": true,
"notNull": true,
"default": "gen_random_uuid()"
},
"title": {
"name": "token_hint",
"type": "varchar",
"primaryKey": false,
"notNull": true
},
}
}
```

Snapshot misunderstand that the length has been changed.

So the drizzle state and the actual dbtable becomes unsynced.

# Expected behavior

Generated sql should change the type of the column like the below.

```sql
ALTER TABLE "posts" RENAME COLUMN "name" TO "title";
ALTER TABLE "posts" ALTER COLUMN "title" SET DATA TYPE varchar;
```

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.