Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: pgEnum generates faulty migrations #3514

Open
1 task done
rnkp755 opened this issue Nov 8, 2024 · 0 comments
Open
1 task done

[BUG]: pgEnum generates faulty migrations #3514

rnkp755 opened this issue Nov 8, 2024 · 0 comments
Labels
bug Something isn't working drizzle/kit

Comments

@rnkp755
Copy link

rnkp755 commented Nov 8, 2024

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.36.1

What version of drizzle-kit are you using?

0.28.0

Other packages

@neondatabase/serverless

Describe the Bug

Created a User Schema with role.

import { pgTable, varchar, timestamp, pgEnum } from "drizzle-orm/pg-core";
import { randomUUID } from "crypto";

// Define the roles enum
export const rolesEnum = pgEnum("roles", ["user", "admin"]);

// Define the 'users' table
export const usersTable = pgTable("users", {
  id: varchar({ length: 36 })
    .$default(() => randomUUID()) // Use Node's randomUUID to generate unique string ID
    .primaryKey(),
  name: varchar({ length: 255 }).notNull(),
  email: varchar({ length: 255 }).notNull().unique(),
  role: rolesEnum().default("user"),
  created_at: timestamp().defaultNow().notNull(),
});

Generated Migration:

CREATE TYPE "public"."roles" AS ENUM('user', 'admin');
CREATE TABLE IF NOT EXISTS "users" (
	"id" varchar(36) PRIMARY KEY NOT NULL,
	"name" varchar(255) NOT NULL,
	"email" varchar(255) NOT NULL,
	"role" "roles" DEFAULT 'user',
	"created_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "users_email_unique" UNIQUE("email")
);

While migrating this an error came as: applying migrations...error: type "roles" does not exist

I needed to change the sql file a bit to make it work perfectly. I added few line of code which should've been added automatically.

New sql file:

CREATE TYPE "public"."roles" AS ENUM('user', 'admin');
CREATE TABLE IF NOT EXISTS "users" (
	"id" varchar(36) PRIMARY KEY NOT NULL,
	"name" varchar(255) NOT NULL,
	"email" varchar(255) NOT NULL,
	"role" "roles" DEFAULT 'user',
	"created_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "users_email_unique" UNIQUE("email")
);
// Extra lines
DO $$ BEGIN
 CREATE TYPE "roles" AS ENUM('admin', 'user');
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
@rnkp755 rnkp755 added the bug Something isn't working label Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working drizzle/kit
Projects
None yet
Development

No branches or pull requests

2 participants