Creating migrations with Enums present (postgres)

Hi! I'm new to drizzle but am trying to create migrations and running into troubles with the auto generation of migration files when an enum is present. For example:
const userStatusEnum = pgEnum('statusEnum', ["pending", "active", "archived"]);
export const users = pgTable('users', {
id: text('id').$default(() => createId()).primaryKey(),
username: text('username').unique().notNull(),
status: userStatusEnum('status').$default(() => "pending"),
});
const userStatusEnum = pgEnum('statusEnum', ["pending", "active", "archived"]);
export const users = pgTable('users', {
id: text('id').$default(() => createId()).primaryKey(),
username: text('username').unique().notNull(),
status: userStatusEnum('status').$default(() => "pending"),
});
When I generate a migration (e.g., npx drizzle-kit generate:pg), I get something like:
-- 0001_awesome_venus.sql
CREATE TABLE IF NOT EXISTS "users" (
"id" text PRIMARY KEY NOT NULL,
"username" text NOT NULL,
"status" "statusEnum",
);
-- 0001_awesome_venus.sql
CREATE TABLE IF NOT EXISTS "users" (
"id" text PRIMARY KEY NOT NULL,
"username" text NOT NULL,
"status" "statusEnum",
);
This fails however as "statusEnum" is not defined which means i need to make the following (manual) modifications to my migration file...
+ DROP TYPE IF EXISTS statusEnum;
+ CREATE TYPE statusEnum AS ENUM ('pending', 'active', 'archived');

CREATE TABLE IF NOT EXISTS "users" (
"id" text PRIMARY KEY NOT NULL,
"username" text NOT NULL,
- "status" "statusEnum",
+ "status" statusEnum DEFAULT 'pending',
);
+ DROP TYPE IF EXISTS statusEnum;
+ CREATE TYPE statusEnum AS ENUM ('pending', 'active', 'archived');

CREATE TABLE IF NOT EXISTS "users" (
"id" text PRIMARY KEY NOT NULL,
"username" text NOT NULL,
- "status" "statusEnum",
+ "status" statusEnum DEFAULT 'pending',
);
Is this anticipated? or is there a way to configure it so that these are done automatically?
2 Replies
ttungu
ttungu10mo ago
everything in schema needs to be exported: export const userStatusEnum = pgEnum('statusEnum', ["pending", "active", "archived"]);
stephen
stephenOP10mo ago
that's such an easy fix! ha!
Want results from more Discord servers?
Add your server