drizzle-team / drizzle-team/drizzle-orm

[BUG]: drizzle-kit for postgres generates incorrect migration when changing IDENTITY column to uuid

Open
#4,178 4 comments 5 reactions 0 assignees View on GitHub
db/postgres drizzle/kit improvement priority
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.40.0

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

0.30.5

### Other packages

_No response_

### Describe the Bug

I made this simple schema in TS
```
import { integer, pgTable, varchar } from "drizzle-orm/pg-core";

export const usersTable = pgTable("users", {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
username: varchar({ length: 255 }).notNull().unique(),
email: varchar({ length: 255 }).notNull().unique(),
});
```
which generates the following SQL code

```
CREATE TABLE "users" (
"id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "users_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1),
"username" varchar(255) NOT NULL,
"email" varchar(255) NOT NULL,
CONSTRAINT "users_username_unique" UNIQUE("username"),
CONSTRAINT "users_email_unique" UNIQUE("email")
);
```

Then I changed the "id" column to be uuid, primary key and have a default random value

```
import { uuid, pgTable, varchar } from "drizzle-orm/pg-core";

export const usersTable = pgTable("users", {
id: uuid().primaryKey().defaultRandom(),
username: varchar({ length: 255 }).notNull().unique(),
email: varchar({ length: 255 }).notNull().unique(),
});
```
which then generated the following SQL code

```
ALTER TABLE "users" ALTER COLUMN "id" SET DATA TYPE uuid;--> statement-breakpoint
ALTER TABLE "users" ALTER COLUMN "id" SET DEFAULT gen_random_uuid();--> statement-breakpoint
ALTER TABLE "users" ALTER COLUMN "id" DROP IDENTITY;
```

However, when migrating via "npx drizzle-kit migrate", I get the following error:
`applying migrations...error: identity column type must be smallint, integer, or bigint
`

As far as I understood it has something to with the fact that the "id" column's type cannot be changed to uuid while it has IDENTITY attribute, but I'm not sure how it should exactly behave.
Thanks!

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.