I need help with Enums in Drizzle.
I referenced two images showing my schema file and the migration generated.
I always get this error userRole does not exist.
3 Replies
Hello, @praiz_dqoder! You have to export your enum and generate the migrations again
still throws error
export const roleEnum = pgEnum('role', ['user', 'admin', 'superAdmin']);
so i renamed userRole to roleEnum and here is the table....
export const users = pgTable(
'users',
{
id: serial('id').primaryKey().notNull(),
name: varchar('name', { length: 70 }).notNull(),
email: varchar('email', { length: 70 }).unique().notNull(),
password: varchar('password', { length: 20 }).notNull(),
role: roleEnum('role').default('user'),
refreshToken: varchar('refresh_token').default(null),
createdAt: timestamp('created_at', {
precision: 6,
withTimezone: true,
}).defaultNow(),
updatedAt: timestamp('updated_at', {
precision: 6,
withTimezone: true,
}).defaultNow(),
},
(users) => {
return {
nameIdx: index('name_idx').on(users.name),
emailIdx: index('email_idx').on(users.email),
};
},
);
//here is the generated migrations
CREATE TABLE IF NOT EXISTS "users" (
"id" serial PRIMARY KEY NOT NULL,
"name" varchar(70) NOT NULL,
"email" varchar(70) NOT NULL,
"password" varchar(20) NOT NULL,
"role" "role" DEFAULT 'user',
"refresh_token" varchar DEFAULT null,
"created_at" timestamp (6) with time zone DEFAULT now(),
"updated_at" timestamp (6) with time zone DEFAULT now(),
CONSTRAINT "users_email_unique" UNIQUE("email")
);
role still doesnt pass as Enum as stated when writing traditional postgres enums.
@praiz_dqoder you have to delete old migration and generate the new one (but be careful with this, unfortunately, there is no rollback functionality now). Because in your migrations there is no creation of enum