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
23.28k stars 568 forks source link

[BUG]: Issue with SQL generation. Trying to reference a type that doesn't exist? #2800

Open maxpaleo opened 1 month ago

maxpaleo commented 1 month ago

What version of drizzle-orm are you using?

1.0.7

What version of drizzle-kit are you using?

No response

Describe the Bug

Recording: https://www.loom.com/share/29310c5b20d24daea7ed212cc10c38f2

Hi all, so I ran into a couple of situations during the migration step, where I am getting the error: applying migrations...error: type "account_type" does not exist

And I believe that this is an issue somewhere within the sql generation.

It looks like the sql syntax is invalid, but then also claims a type that does exist does not.

I've explained it in detail in the video above; I could be wrong so if I am, let me know what it is.

So far I've always resolved this by manually running the sql updates.

Expected behavior

No response

Environment & setup

No response

ZarkoPernar commented 1 month ago

I ran into similar issues with enums I was not exporting from the schema file. Check your generated sql file for use of "account_type" and then check if it is defined anywhere

AlexBeauchemin commented 1 month ago

I ran into the same issue and I also believe there's an issue with SQL generation and enums For me, prefixing "public" to the enum name seems to fix the issue ie:

ALTER TYPE "account_type" RENAME VALUE 'something' TO 'something-else'; // generated by drizzle
ALTER TYPE "public"."account_type" RENAME VALUE 'something' TO 'something-else'; // now it works

So I copy/pasted the changes (from the generated migration) in a custom migration file while adding this "public" prefix to all enum statements

maxpaleo commented 1 month ago

Hi @ZarkoPernar , yes, I did check this, but the enums were exported. I ran different tests to see what the issue could have been, but Drizzle did not identify these enum declarations at any point. It's not the first time i run into this, i've had to manually run sql commands for updates on several occasions and then uncomment lines in the migration files.

maxpaleo commented 3 weeks ago

Added a new message as it just happened again. I reference an exported enum in a table but it can't find it and won't ever create automatically.

Screenshot 2024-08-24 at 12 16 16 AM Screenshot 2024-08-24 at 12 17 17 AM