Having Issue with Enum in Neon Postgres

I have been trying to have a schema of this kind
export const userRoleEnum = pgEnum('user_role', ['user', 'admin']);

export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 255 }).notNull(),
email: varchar('email', { length: 255 }).notNull().unique(),
role: userRoleEnum().notNull().default('user'),
});
export const userRoleEnum = pgEnum('user_role', ['user', 'admin']);

export const users = pgTable('users', {
id: serial('id').primaryKey(),
name: varchar('name', { length: 255 }).notNull(),
email: varchar('email', { length: 255 }).notNull().unique(),
role: userRoleEnum().notNull().default('user'),
});
so when I give generate *cmd * drizzle-kit generate works but push *cmd * drizzle-kit push gives me error
error: column "role" cannot be cast automatically to type user_role
at D:\yt_upload_saas\node_modules\pg-pool\index.js:45:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.query (D:\yt_upload_saas\node_modules\drizzle-kit\bin.cjs:78762:26)
at async pgPush (D:\yt_upload_saas\node_modules\drizzle-kit\bin.cjs:82241:13)
at async Object.handler (D:\yt_upload_saas\node_modules\drizzle-kit\bin.cjs:92191:9)
at async run (D:\yt_upload_saas\node_modules\drizzle-kit\bin.cjs:90501:7) {
length: 183,
severity: 'ERROR',
code: '42804',
detail: undefined,
hint: 'You might need to specify "USING role::user_role".',
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'tablecmds.c',
line: '12520',
routine: 'ATPrepAlterColumnType'
error: column "role" cannot be cast automatically to type user_role
at D:\yt_upload_saas\node_modules\pg-pool\index.js:45:11
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Object.query (D:\yt_upload_saas\node_modules\drizzle-kit\bin.cjs:78762:26)
at async pgPush (D:\yt_upload_saas\node_modules\drizzle-kit\bin.cjs:82241:13)
at async Object.handler (D:\yt_upload_saas\node_modules\drizzle-kit\bin.cjs:92191:9)
at async run (D:\yt_upload_saas\node_modules\drizzle-kit\bin.cjs:90501:7) {
length: 183,
severity: 'ERROR',
code: '42804',
detail: undefined,
hint: 'You might need to specify "USING role::user_role".',
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'tablecmds.c',
line: '12520',
routine: 'ATPrepAlterColumnType'
2 Replies
TOSL
TOSL6d ago
Did you change from a different column type to enum? Manually altering this column should the issue. I think you can do this in drizzle studio or probably neon if they have a sql runner you can use.
ALTER TABLE users
ALTER COLUMN role TYPE user_role USING role::user_role;
ALTER TABLE users
ALTER COLUMN role TYPE user_role USING role::user_role;
should fix it
Rahul aithal
Rahul aithalOP3d ago
I did this but it didn't work so i had to drop the table, then worked

Did you find this page helpful?