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.54k stars 643 forks source link

[BUG]: #2580

Closed jcoulaud closed 1 month ago

jcoulaud commented 4 months ago

What version of drizzle-orm are you using?

0.31.2

What version of drizzle-kit are you using?

0.22.7

Describe the Bug

Hello there!

I created an enum in my schema:

export const statusEnum = pgEnum('status', ['monitored', 'bought', 'sold', 'dropped']);
export const tokens = pgTable('tokens', {
  status: statusEnum('status'),
});

, but it seems that only the first 3 are taking into account. The migration appears correct though:

DO $$ BEGIN
 CREATE TYPE "public"."status" AS ENUM('monitored', 'bought', 'sold', 'dropped');
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE TABLE IF NOT EXISTS "tokens" (
    "status" "status",
);

But I look at my interface, I don't see the value dropped, and when I try to update my status with the dropped enum, I get an error: error: invalid input value for enum status: "dropped" Screenshot 2024-07-02 at 16 51 10@2x

Expected behavior

No response

Environment & setup

No response

RomanNabukhotnyi commented 1 month ago

@jcoulaud Hey! It seems that the database already had a type with this name and only 3 values ​​and it did not return an error about a duplicate. You can check this by running this query:

SELECT 
     n.nspname as enum_schema,
     t.typname as enum_name,
     e.enumlabel as enum_value
FROM pg_type t
JOIN pg_enum e ON t.oid = e.enumtypid
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace;

If so, delete the type and run the migration again.

jcoulaud commented 1 month ago

@jcoulaud Hey! It seems that the database already had a type with this name and only 3 values ​​and it did not return an error about a duplicate. You can check this by running this query:

SELECT 
     n.nspname as enum_schema,
     t.typname as enum_name,
     e.enumlabel as enum_value
FROM pg_type t
JOIN pg_enum e ON t.oid = e.enumtypid
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace;

If so, delete the type and run the migration again.

Hey Roman, I’ve updated my schema since then, but I remember that deleting the type and rerunning the migration did the trick. The key was to delete the type.