[BUG]: pgEnum -> type "xxx" already exists during migration
What version of drizzle-orm are you using?
"^0.35.3"
What version of drizzle-kit are you using?
"^0.26.2"
Describe the Bug
Hi In a nextjs app with "drizzle-orm/pg-core" i have this enum
export const xxxEnum = pgEnum("xxx", [
"x1",
"x2",
"x3"
]);
And when I migrate my schemas first time all is ok, but after a new migration i got the error
❌ Migration failed PostgresError: type "xxx" already exists at ErrorResponse (D:\Work\webapps\damhub\node_modules\postgres\cjs\src\connection.js:788:26) at handle (D:\Work\webapps\damhub\node_modules\postgres\cjs\src\connection.js:474:6) at Socket.data (D:\Work\webapps\damhub\node_modules\postgres\cjs\src\connection.js:315:9) at Socket.emit (node:events:519:28) at addChunk (node:internal/streams/readable:559:12) at readableAddChunkPushByteMode (node:internal/streams/readable:510:3) at Readable.push (node:internal/streams/readable:390:5)
If I do a db: push all works fine.
regards
Expected behavior
I expected a success migration after new migration. Not only the first time
Environment & setup
This is my package { "name": "xxx", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev --turbo", "build": "next build", "start": "next start", "lint": "next lint", "db:generate": "drizzle-kit generate", "db:migrate": "tsx lib/db/migrate.ts", "db:drop": "drizzle-kit drop", "db:pull": "drizzle-kit introspect", "db:push": "drizzle-kit push", "db:studio": "drizzle-kit studio", "db:check": "drizzle-kit check" }, "dependencies": { "@kinde-oss/kinde-auth-nextjs": "^2.4.3", "@radix-ui/react-avatar": "^1.1.1", "@radix-ui/react-checkbox": "^1.1.2", "@radix-ui/react-collapsible": "^1.1.1", "@radix-ui/react-dialog": "^1.1.2", "@radix-ui/react-dropdown-menu": "^2.1.2", "@radix-ui/react-icons": "^1.3.0", "@radix-ui/react-label": "^2.1.0", "@radix-ui/react-separator": "^1.1.0", "@radix-ui/react-slot": "^1.1.0", "@radix-ui/react-tooltip": "^1.1.3", "@t3-oss/env-nextjs": "^0.11.1", "class-variance-authority": "^0.7.0", "clsx": "^2.1.1", "dotenv": "^16.4.5", "drizzle-orm": "^0.35.3", "drizzle-zod": "^0.5.1", "lucide-react": "^0.453.0", "nanoid": "^5.0.7", "next": "14.2.16", "next-themes": "^0.3.0", "postgres": "^3.4.4", "react": "18.3.1", "react-dom": "18.3.1", "sonner": "^1.5.0", "tailwind-merge": "^2.5.4", "tailwindcss-animate": "^1.0.7", "zod": "^3.23.8" }, "devDependencies": { "@types/node": "^20", "@types/react": "^18", "@types/react-dom": "^18", "drizzle-kit": "^0.26.2", "eslint": "^8", "eslint-config-next": "14.2.15", "postcss": "^8", "tailwindcss": "^3.4.1", "tsx": "^4.19.1", "typescript": "^5" } }
[email protected] also has this problem
[email protected] has the same problem
export function enumToPgEnum<T extends Record<string, any>>(
myEnum: T,
): [T[keyof T], ...T[keyof T][]] {
return Object.values(myEnum).map((value: any) => `${value}`) as any
}
export enum USER_ROLE {
ADMIN = 'ADMIN',
USER = 'USER',
}
export const userRoleEnum = pgEnum('user_role', enumToPgEnum(USER_ROLE))
export const user = pgTable('user', {
id: serial('id').primaryKey(),
email: varchar('email').notNull().unique(),
role: userRoleEnum().default(USER_ROLE.USER).notNull(),
running migrate ends up with:
Using 'pg' driver for database querying
[⣷] applying migrations...error: type "user_role" already exists
at /Users/user/Desktop/Projects/api/node_modules/pg/lib/client.js:535:17
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async <anonymous> (/Users/user/Desktop/Projects/api/node_modules/src/pg-core/dialect.ts:102:7)
at async NodePgSession.transaction (/Users/user/Desktop/Projects/api/node_modules/src/node-postgres/session.ts:193:19)
at async PgDialect.migrate (/Users/user/Desktop/Projects/api/node_modules/src/pg-core/dialect.ts:95:3)
at async migrate (/Users/user/Desktop/Projects/api/node_modules/src/node-postgres/migrator.ts:10:2) {
length: 89,
severity: 'ERROR',
code: '42710',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'typecmds.c',
line: '1169',
routine: 'DefineEnum'
}
[email protected] also has the same problem. Push works, but generate+migrate does not.
length: 84,
severity: 'ERROR',
code: '42710',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'typecmds.c',
line: '1177',
routine: 'DefineEnum'
Still a problem.
Has anyone found the solution to this ?
I am facing problem. even i just created the enum and run the migration but first one failed due to other reasones , and then when i run again it is showing this error.
] applying migrations...error: type "status" already exists
Any known fix for this yet? This is a very annoying bug; failing all the migrations.
Also getting this bug, how can this still be an issue?
Ok here's the fix, it's actually pretty easy. Go into your .sql migration file where it says something like
CREATE TYPE "public"."user_global_role" AS ENUM('SUPERADMIN', 'CUSTOMER');
and wrap it with a BEGIN EXCEPTION statement like this:
DO $$ BEGIN
CREATE TYPE "public"."user_global_role" AS ENUM('SUPERADMIN', 'CUSTOMER');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
Still, drizzle-kit should handle this.
The Exception fix did not work for me. Any other ways to fix this?
same for me also not working
got this issue
Also got this
same issue
had to write it like that, then it worked:
DO $$
BEGIN
CREATE TYPE "public"."Role" AS ENUM('OWNER', 'EDITOR', 'VIEWER');
EXCEPTION
WHEN duplicate_object THEN
NULL;
END $$;
had to write it like that, then it worked:
DO $$ BEGIN CREATE TYPE "public"."Role" AS ENUM('OWNER', 'EDITOR', 'VIEWER'); EXCEPTION WHEN duplicate_object THEN NULL; END $$;
I've been facing the same issue with drizzle-kit migrate, and the above workaround still doesn't work.
I experience this as well.
this seems to work, in my case wasnt just the enum! everything so i had to wrap the whole sql in it and then the migration works
BEGIN
CREATE TYPE "public"."test" AS ENUM('test1', 'test2');
EXCEPTION
WHEN duplicate_object THEN null;
END
$$;
this seems to work, in my case wasnt just the enum! everything so i had to wrap the whole sql in it and then the migration works
BEGIN CREATE TYPE "public"."test" AS ENUM('test1', 'test2'); EXCEPTION WHEN duplicate_object THEN null; END $$;
is the DO $$ unnecessary anymore?
I had a problem running it with it which unterminated dollar-quoted string at or near "$$...
and without it with syntax error at or near "CREATE"
I'm not familiar with postgres as it was my first time trying and learn it.
broken for me too
drizzle-kit v0.31.1, the issue is still present
Ok here's the fix, it's actually pretty easy. Go into your .sql migration file where it says something like
CREATE TYPE "public"."user_global_role" AS ENUM('SUPERADMIN', 'CUSTOMER');and wrap it with a BEGIN EXCEPTION statement like this:
DO $$ BEGIN CREATE TYPE "public"."user_global_role" AS ENUM('SUPERADMIN', 'CUSTOMER'); EXCEPTION WHEN duplicate_object THEN null; END $$; Still, drizzle-kit should handle this.
Only this helped me
DROP TYPE IF EXISTS "public"."test";
CREATE TYPE "public"."test" AS ENUM('test1', 'test2');
If you can't drop the type because you have tables that depend on it, I found out just removing the CREATE TYPE definition also works, so you're welcome
Hey everyone!
I've created this message to send in a batch to all opened issues we have, just because there are a lot of them and I want to update all of you with our current work, why issues are not responded to, and the amount of work that has been done by our team over ~8 months.
I saw a lot of issues with suggestions on how to fix something while we were not responding – so thanks everyone. Also, thanks to everyone patiently waiting for a response from us and continuing to use Drizzle!
We currently have 4 major branches with a lot of work done. Each branch was handled by different devs and teams to make sure we could make all the changes in parallel.
First branch is drizzle-kit rewrite
All of the work can be found on the alternation-engine branch. Here is a PR with the work done: https://github.com/drizzle-team/drizzle-orm/pull/4439
As you can see, it has 167k added lines of code and 67k removed, which means we've completely rewritten the drizzle-kit alternation engine, the way we handle diffs for each dialect, together with expanding our test suite from 600 tests to ~9k test units for all different types of actions you can do with kit. More importantly, we changed the migration folder structure and made commutative migrations, so you won't face complex conflicts on migrations when working in a team.
What's left here:
- We are finishing handling defaults for Postgres, the last being geometry (yes, we fixed the
sridissue here as well). - We are finishing commutative migrations for all dialects.
- We are finishing up the command, so the migration flow will be as simple as
drizzle-kit upfor you.
Where it brings us:
- We are getting drizzle-kit into a new good shape where we can call it
[email protected]!
Timeline:
- We need ~2 weeks to finish all of the above and send this branch to beta for testing.
Second big branch is a complex one with several HUGE updates
- Bringing Relational Queries v2 finally live. We've done a lot of work here to actually make it faster than RQBv1 and much better from a DX point of view. But in implementing it, we had to make another big rewrite, so we completely rewrote the drizzle-orm type system, which made it much simpler and improved type performance by ~21.4x:
(types instantiations for 3300 lines production drizzle schema + 990 lines relations)
TS v5.8.3: 728.8k -> 34.1k
TS v5.9.2: 553.7k -> 25.4k
You can read more about it here.
What's left here:
- We have 1 issue with TS that is already in progress of being fixed. The issue and Post about fixing.
Where it brings us:
- We are getting drizzle-orm into a new good shape where we can call it
[email protected]!
Breaking changes:
- We will have them, but we will have open channels for everyone building on top of drizzle types, so we can guide you through all the changes.
Third branch is adding support for CockroachDB and MSSQL dialects
Support for them is already in the alternation-engine branch and will be available together with the drizzle-kit rewrite.
Summary
All of the work we are doing is crucial and should be done sooner rather than later. We've received a lot of feedback and worked really hard to find the best strategies and decisions for API, DX, architecture, etc., so we can confidently mark it as v1 and be sure we can improve it and remain flexible for all the features you are asking for, while becoming even better for everyone building on top of the drizzle API as well.
We didn't want to stay with some legacy decisions and solutions we had, and instead wanted to shape Drizzle in a way that will be best looking ahead to 2025–2026 trends (v1 will get proper effect support, etc.).
We believe that all of the effort we've put in will boost Drizzle and benefit everyone using it.
Thanks everyone, as we said, we are here to stay for a long time to build a great tool together!
Timelines
We are hoping to get v1 for drizzle in beta this fall and same timeline for latest. Right after that we can go through all of the issues and PRs and resond everyone. v1 for drizzle should close ~70% of all the bug tickets we have, so on beta release we will start marking them as closed!
Ok here's the fix, it's actually pretty easy. Go into your .sql migration file where it says something like
CREATE TYPE "public"."user_global_role" AS ENUM('SUPERADMIN', 'CUSTOMER');and wrap it with a BEGIN EXCEPTION statement like this:
DO $$ BEGIN CREATE TYPE "public"."user_global_role" AS ENUM('SUPERADMIN', 'CUSTOMER'); EXCEPTION WHEN duplicate_object THEN null; END $$; Still, drizzle-kit should handle this.
This worked for me!
I also have to remove --> statement-breakpoint
The enum option in a text works with generate + migrate.
For example,
const users = pgTable("users", {
roles: text("roles", { enum: ["ADMIN", "SUPERADMIN", "CUSTOMER"] }),
});