drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.55k stars 643 forks source link

[BUG]: drizzle-kit push is not detecting Postgres enum label already exists #2389

Closed matowang closed 4 weeks ago

matowang commented 5 months ago

What version of drizzle-orm are you using?

0.30.9

What version of drizzle-kit are you using?

0.21.2

Describe the Bug

This bug happens every so often. Is there a work around?

pnpm drizzle-kit push drizzle-kit: v0.21.2 drizzle-orm: v0.30.9

No config path provided, using default path

Using 'pg' driver for database querying

Warning You are about to execute current statements:

ALTER TYPE "job_name" ADD VALUE 'SET_PCS_POWER_KW';

error: enum label "SET_PCS_POWER_KW" already exists

kit@0.21.2/node_modules/drizzle-kit/bin.cjs:113632:7) { length: 102, 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: 'pg_enum.c', line: '293', routine: 'AddEnumLabel' }

Expected behavior

No response

Environment & setup

No response

RomanBaiocco commented 5 months ago

I'm also running into this issue. I am using drizzle to manage two different instances of a pg db, one for staging and another for prod. I can push to the staging instance but not the prod instance. If i remove the enum value it complains about from my schema, I can push to prod, but if I add it back I still encounter the same error

Lukem121 commented 5 months ago

I don't know how to fix it but I believe you can reproduce it by creating a Postgres enum, pushing it to the database, then if you add new items, or shuffle the tuple, then when you push again it will error.

motopods commented 5 months ago

I don't know how to fix it but I believe you can reproduce it by creating a Postgres enum, pushing it to the database, then if you add new items, or shuffle the tuple, then when you push again it will error.

You're right, the order of the enum definitions in the database must be consistent with the definitions in the Drizzle ORM, otherwise this error will occur.

JohnCido commented 5 months ago

I have just upagraded drizzle-kit to latest. And this happens when I try to run push to my dev db.

mattiasbonte commented 5 months ago

+1 upgraded this morning because I needed the geometry type and now can't seem to do a migrate because of this. Has anyone found a solution/workaround?

JohnCido commented 5 months ago

Is there any update on this? As a workaround I can mannually run the statements that does not involve creating duplicate enum labels by using verbose mode of push command. But I believe it has to be a drizzle-kit bug since it tries to add enum labels that not only defined but also used in tabels.

imoshimuir commented 5 months ago

I'm also running into this issue with enums, since upgrading (also to get the geometry types)

hlspablo commented 5 months ago

someone managed to solve this? UPDATE: the workaround was deleting manually the enum ( and remove all references to it on schema ) and run push again with the enums added

MickL commented 4 months ago

Already 19 upvotes on this issue. Unfortunately I feel like Drizzle Team is not reading any Github issue :(

Having the same problem: Added a new value to the enum but Drizzle trys to add existing enums resulting in an error:

Warning  You are about to execute current statements:

ALTER TYPE "product-type" ADD VALUE 'bracelet';
ALTER TYPE "product-type" ADD VALUE 'wristwatch';
ALTER TYPE "product-type" ADD VALUE 'necklace';
ALTER TYPE "product-type" ADD VALUE 'pendant';
ALTER TYPE "product-type" ADD VALUE 'ring';

error: enum label "bracelet" already exists
JohnCido commented 4 months ago

And this is the related issue for drizzle-kit-mirror repo.

MickL commented 4 months ago

This issue seems to be a different one. Maybe we should reopen the same issue there because the dont seem to read any Github issues.

hlspablo commented 4 months ago

unfortunately this project is taking the same direction of Prisma, that is, adding lot of features without taking care of the basics of the framework, that's why JS frameworks are so easily replaced and forgotten, this issue is proof of this. I'm not complaining since it is open source, just pointing it

MickL commented 4 months ago

Eventually every one of us has the opportunity to fix the issue and make a PR tho

NEVERMIND, the Drizzle Kit repo seems to be private. I just wanted to see if I can fix it but I cant find the repository ...

MickL commented 4 months ago

I just realized that the order of the enum makes a difference! If you add values but dont change the order Drizzle Kit is pushing the new values. If you change the order Drizzle Kit wants to add new values that already exist. So probably also a quick bug fix if the repo would be public...

Please also be sure to upvote the following issue, maybe they look into the issues there? https://github.com/drizzle-team/drizzle-orm/issues/3305

JohnCido commented 4 months ago

Eventually every one of us has the opportunity to fix the issue and make a PR tho

NEVERMIND, the Drizzle Kit repo seems to be private. I just wanted to see if I can fix it but I cant find the repository ...

Yes, I only realized that the drizzle-kit is not fully open source once this issue oocured.

arvilmena commented 3 months ago

did anyone found a workaround while waiting for the Drizzle team to fix this? I fear my migrations meta definitions will be bricked if I just execute the generated SQL migration directly.

MickL commented 3 months ago

Dont change the order and it should work correctly, at least it did for me

NickWoodward commented 3 months ago

Dont change the order and it should work correctly, at least it did for me

Not all heroes wear capes.

But yup, same for me. If you insert enums it breaks drizzle-kit, if you append them it works fine

enisze commented 3 months ago

would love a fix for this

AndriiSherman commented 3 months ago

Please try drizzle-kit@enums_fix tag. Should be fixed there. I'll include a fix in 0.23.2 release today

AndriiSherman commented 3 months ago

Eventually every one of us has the opportunity to fix the issue and make a PR tho

NEVERMIND, the Drizzle Kit repo seems to be private. I just wanted to see if I can fix it but I cant find the repository ...

https://github.com/drizzle-team/drizzle-orm/pull/2711

Will merge it today

NickBolles commented 3 months ago

@AndriiSherman The second issue that I've run into in the last hour that already has a fix out! drizzle-kit@enums_fix fixed it for me. 🎉 thank you @AndriiSherman !

NickWoodward commented 3 months ago

Please try drizzle-kit@enums_fix tag. Should be fixed there. I'll include a fix in 0.23.2 release today

thanks!

AndriiSherman commented 3 months ago

was included and fixed in drizzle-kit@0.23.2

Yorkemartin commented 3 months ago

ive still got this error happening, with drizzle-kit@0.24.0, and drizzle-orm@0.33.0

pencilcheck commented 3 months ago

yea, the latest drizzle hasn't fixed the ordering issue, you have to look up the ordering of the enum type and simply make sure the order is correct in your code as well then the error goes away, and any new enum you have to add to the bottom of the list.

MickL commented 3 months ago

I dont understand. What did the update fix exactly? Wasnt a new order after adding one property to the enum which is not at the end the main problem?

ironbyte commented 3 months ago

Any ETA on a fix for this annoying bug?

farhan-helmy commented 3 months ago

upgraded to latest, still the same bug

Yorkemartin commented 3 months ago

I had to downgrade to fix

Yorkemartin commented 3 months ago

Someone should mark this issue still open.

MickL commented 3 months ago

Seriously I think they dont look into the issues at all @AndriiSherman

CallumGracie95 commented 2 months ago

image

maxpaj commented 2 months ago

What's a workaround for it? Downgrade to what version exactly?

emilio-toledo commented 2 months ago

I have just experienced this issue too

mogery commented 2 months ago

Can confirm, still broken.

MickL commented 2 months ago

Is anyone reading this from the Drizzle Team?? @AndriiSherman please reopen

Also can we have Drizzle Kit open source so we can fix things ourself? Drizzle team obviously not reading the Github issues

GT-Eric commented 1 month ago

Dont change the order and it should work correctly, at least it did for me

This is the solution btw

ThatFruedDued commented 1 month ago

This is not a solution, I wanted to make a design change which involved reordering enums and ran into this issue.

gokaygurcan commented 1 month ago

Saga continues..

turbo db:push --filter database                                                                                                                          git:(dev|✚10…8⚑7
• Packages in scope: database
• Running db:push in 1 packages
• Remote caching enabled
database:db:push: cache hit, replaying logs 3c12a53e155654cb
database:db:push: 
database:db:push: $ drizzle-kit push --config=drizzle.config.ts --force
database:db:push: Reading config file '/Users/gokaygurcan/projects/test/packages/database/drizzle.config.ts'
database:db:push: Using 'pg' driver for database querying
[✓] Pulling schema from database...
database:db:push: 
database:db:push:  Warning  You are about to execute current statements:
database:db:push: 
database:db:push: ALTER TYPE "role" ADD VALUE 'admin';
database:db:push: 
database:db:push: error: enum label "role" already exists
database:db:push:     at /Users/gokaygurcan/projects/test/node_modules/pg-pool/index.js:45:11
database:db:push:     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
database:db:push:     at async Object.query (/Users/gokaygurcan/projects/test/node_modules/drizzle-kit/bin.cjs:71880:26)
database:db:push:     at async pgPush (/Users/gokaygurcan/projects/test/node_modules/drizzle-kit/bin.cjs:74835:13)
database:db:push:     at async Object.handler (/Users/gokaygurcan/projects/test/node_modules/drizzle-kit/bin.cjs:83713:9)
database:db:push:     at async run (/Users/gokaygurcan/projects/test/node_modules/drizzle-kit/bin.cjs:82064:7) {
database:db:push:   length: 96,
database:db:push:   severity: 'ERROR',
database:db:push:   code: '42710',
database:db:push:   detail: undefined,
database:db:push:   hint: undefined,
database:db:push:   position: undefined,
database:db:push:   internalPosition: undefined,
database:db:push:   internalQuery: undefined,
database:db:push:   where: undefined,
database:db:push:   schema: undefined,
database:db:push:   table: undefined,
database:db:push:   column: undefined,
database:db:push:   dataType: undefined,
database:db:push:   constraint: undefined,
database:db:push:   file: 'pg_enum.c',
database:db:push:   line: '348',
database:db:push:   routine: 'AddEnumLabel'
database:db:push: }

 Tasks:    1 successful, 1 total
Cached:    1 cached, 1 total
  Time:    174ms >>> FULL TURBO

Well, at least it fails fast.

Can someone remind me why this issue was closed again?

AndriiSherman commented 1 month ago

We are merging a fix to add labels, and we will also include fixes for reordering and other enum manipulations. Additionally, reordering of enum values will involve dropping the enum, altering the column type to string, adding the new enum, and then changing the column type back to the updated enum

AndriiSherman commented 1 month ago

one PR: https://github.com/drizzle-team/drizzle-orm/pull/3076 another PR will be included soon

AndriiSherman commented 1 month ago

Please try drizzle-kit@0.26.0

sarthakroy107 commented 1 month ago

I just realized that the order of the enum makes a difference! If you add values but dont change the order Drizzle Kit is pushing the new values. If you change the order Drizzle Kit wants to add new values that already exist. So probably also a quick bug fix if the repo would be public...

Please also be sure to upvote the following issue, maybe they look into the issues there? drizzle-team/drizzle-orm#3305

Worked for me

ironbyte commented 1 month ago

Please try drizzle-kit@0.26.0

Hey, can confirm it fixes the issue for me (After I made changes in order to trigger the issue before updating my Drizzle packages). Thanks <3

matowang commented 3 weeks ago

@AndriiSherman after upgrading to drizzle-kit@0.26.0 it seems to want to recreate the types. This will fail since the types already exist.

Is there a possible work around?

image
Ricardosgeral commented 3 weeks ago

I have the same issue on the enum

error: type "..." already exists.

This needs correction

olestole commented 3 weeks ago

I thought I was experiencing the same issue, but it happened that the enum actually existed as the migration-files had been overwritten directly. Run this query in the database ensure the enum doesn't exist:

SELECT e.enumlabel AS enum_value
FROM pg_type t
JOIN pg_enum e ON t.oid = e.enumtypid
WHERE t.typname = <YOUR_ENUM>;
Ricardosgeral commented 3 weeks ago

i still have this issue Ricardo Santos

On Mon, Oct 28, 2024 at 12:20 PM Ole August Støle @.***> wrote:

I thought I was experiencing the same issue, but it happened that the enum actually existed as the migration-files had been overwritten directly. Run this query in the database ensure the enum doesn't exist:

SELECT e.enumlabel AS enum_value FROM pg_type t JOIN pg_enum e ON t.oid = e.enumtypid WHERE t.typname = 'transactionTypeEnum';

— Reply to this email directly, view it on GitHub https://github.com/drizzle-team/drizzle-orm/issues/2389#issuecomment-2441434253, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHIREUF37WS4LOMGF5PVHXLZ5YMXTAVCNFSM6AAAAABIOBK2YGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDINBRGQZTIMRVGM . You are receiving this because you commented.Message ID: @.***>

charlietlamb commented 1 week ago

getting this error with drizzle-kit@0.28.0